Use VB.NET to make Winform (original) for configuring web.config features

xiaoxiao2021-03-06  53

The interface is simple, five controls TXTSERVERNAME, CBODATABASE, TXTUSERNAME, TXTPASSWORD, CBWINDOWSAUTHENTICATION

Below is the VB.NET background code:

Imports SystemImports System.diagnosticsimports System.ConfigurationImports System.xmlimports System.TextImports System.Reflection

Public Class frmmain ??? inherits system.windows.forms.form

#Region "Windows Form Designer Generated Code"

??? public sub new () ??????? mybase.new ()

??????? 'This call is required for the Windows Form Designer. ??????? InitializeComponent ()

??????? 'Add any initialization after INITIALIZECOMPONENT () call

??? End Sub

??? 'Form Rewinds Dispose to clean the component list. ??? protected overloads overrides sub dispose (byval disposing as boolean) ??????? if disposing the ??????????? if not (Components Is Nothing) THEN ??????? ????????????????????? End if ??????? end if ??????? mybase.dispose (Disposing) ?? ? End Sub

??? 'Windows Form Designer Needly ??? Private Components as System.comPonentModel.icontainer

??? 'Note: The following procedure is necessary for the Windows Form Designer to modify this process using the Windows Form Designer. ?? 'Don't modify it using the code editor. ??? Friend WithEvents GroupBox1 As System.Windows.Forms.GroupBox ??? Friend WithEvents txtServerName As System.Windows.Forms.TextBox ??? Friend WithEvents cbWindowsAuthentication As System.Windows.Forms.CheckBox ??? Friend WithEvents grpLogin As System .Windows.Forms.GroupBox ??? Friend WithEvents lblPassword As System.Windows.Forms.Label ??? Friend WithEvents lblUserName As System.Windows.Forms.Label ??? Friend WithEvents txtPassword As System.Windows.Forms.TextBox ?? ? Friend WithEvents txtUserName As System.Windows.Forms.TextBox ??? Friend WithEvents grbDatabase As System.Windows.Forms.GroupBox ??? Friend WithEvents cboDatabase As System.Windows.Forms.ComboBox ??? Friend WithEvents btnCancel As System.Windows . Form.Button ??? Friend Withevents btnok as system.windows.Forms.Button ??? Friend Withevents LBLSERVER As System.Windows.Forms.Label ??? private subinitiZeComponent ()

??????? me.groupbox1 = new system.windows.Forms.groupbox

??????? me.txtServerName = new system.windows.Forms.TextBox

??????? me.cbwindowsauthentication = new system.windows.Forms.checkbox

??????? me.grplogin = new system.windows.forms.groupbox

??????? me.lblpassword = new system.windows.forms.label

??????? me.lblusername = new system.windows.forms.label

??????? me.txtpassword = new system.windows.Forms.TextBox

??????? me.txtusername = new system.windows.Forms.TextBox

??????? me.grbdatabase = new system.windows.Forms.groupbox

??????? me.cbodatabase = new system.windows.Forms.comBobox

??????? me.btncel = new system.windows.Forms.Button

??????? me.btnok = new system.windows.Forms.Button

??????? me.lblserver = new system.windows.forms.Label ??????? me.groupbox1.suspendlayout ()

??????? me.grplaogin.suspendlayout ()

??????? me.grbdatabase.suspendlayout ()

??????? me.suspendlayout ()

??????? '

??????? 'groupbox1

??????? '

??????? me.groupbox1.controls.add (me.txtservername)

??????? me.groupbox1.controls.add (me.cbwindowsauthentication)

??????? me.groupBox1.controls.add (me.grplogin)

??????? me.groupBox1.controls.add (me.grbdatabase)

??????? me.groupbox1.controls.add (me.btncel)

??????? me.groupbox1.controls.add (me.btnok)

??????? me.groupbox1.controls.add (me.lblserver)

??????? me.groupbox1.location = new system.drawing.point (8, 8)

??????? me.groupbox1.name = "groupbox1"

??????? me.groupbox1.size = new system.drawing.size (321, 237)

??????? me.groupbox1.tabindex = 15

??????? me.groupbox1.tabstop = false

??????? me.groupbox1.text = "Database Settings"

??????? '

??????? 'txtServername

??????? '

??????? me.txtServername.Location = new system.drawing.point (115, 30)

??????? me.txtServerName.name = "txtservername"

??????? me.txtServername.size = new system.drawing.size (178, 21)

??????? me.txtServername.tabindex = 10

?????? me.txtServername.text = ""

??????? '

??????? 'CBWindowsAuthentication

??????? '

??????? me.cbwindowsauthentication.location = new system.drawing.point (38, 60)

??????? me.cbwindowsauthentication.name = "cbwindowsauthentication"

??????? me.cbwindowsauthentication.size = new system.drawing.size (173, 22)

??????? me.cbwindowsauthentication.tabindex = 1

??????? me.cbwindowsauthentication.text = "Use Windows Verification"

??????? '

??????? 'grplogin ???????

??????? me.grplogin.controls.add (me.lblpassword)

??????? me.grplogin.controls.add (me.lblusername)

??????? me.grplogin.controls.add (me.txtpassword)

??????? me.grplogin.controls.add (me.txtusername)

??????? me.grplaogin.location = new system.drawing.point (29, 65)

??????? me.grplogin.name = "grplogin"

??????? me.grplaogin.size = new system.drawing.size (269, 73)

??????? me.grplogin.tabindex = 2

??????? me.grplaogin.tabstop = false

??????? '

??????? 'lblpassword

??????? '

??????? me.lblpassword.location = new system.drawing.point (10, 43)

??????? me.lblpassword.name = "lblpassword"

??????? me.lblpassword.size = new system.drawing.size (76, 22)

??????? me.lblpassword.tabindex = 13

??????? me.lblpassword.text = "password"

??????? me.lblpassword.textalign = system.drawing.contentAlignment.middleright

??????? '

??????? 'lblusername

??????? '

??????? me.lblusername.location = new system.drawing.point (10, 17)

??????? me.lblusername.name = "lblusername"

??????? me.lblusername.size = new system.drawing.size (76, 26)

??????? me.lblusername.tabindex = 14

??????? me.lblusername.text = "user"

??????? me.lblusername.textalign = system.drawing.contentAlignment.middleright

??????? '

??????? 'txtpassword

??????? '

??????? me.txtpassword.location = new system.drawing.point (86, 43)

??????? me.txtpassword.name = "txtpassword"

??????? me.txtpassword.passwordchar = microsoft.visualbasic.chrw (42)

??????? me.txtpassword.size = new system.drawing.size (173, 21)

??????? me.txtpassword.tabindex = 4

??????? me.txtpassword.text = "" ??????? '

??????? 'txtusername

??????? '

??????? me.txtusername.location = new system.drawing.point (86, 17)

??????? me.txtusername.name = "txtusername"

??????? me.txtusername.size = new system.drawing.size (173, 21)

??????? me.txtusername.tabindex = 3

??????? me.txtusername.text = ""

??????? '

??????? 'grbdatabase

??????? '

??????? me.grbdatabase.controls.add (me.cbodatabase)

??????? me.grbdatabase.location = new system.drawing.point (29, 142)

??????? me.grbdatabase.name = "grbdatabase"

??????? me.grbdatabase.size = new system.drawing.size (269, 52)

??????? me.grbdatabase.tabindex = 5

??????? me.grbdatabase.tabstop = false

??????? me.grbdatabase.text = "Database"

??????? '

??????? 'CBODATABASE

??????? '

??????? me.cbodatabase.ItemHeight = 12

??????? me.cbodatabase.location = new system.drawing.point (14, 22)

??????? me.cbodatabase.name = "CBODATABASE"

??????? me.cbodatabase.size = new system.drawing.size (240, 20)

??????? me.cbodatabase.tabindex = 6

??????? '

??????? 'btncel

??????? '

??????? me.btncel.location = new system.drawing.point (176, 202)

??????? me.btncel.name = "btncel"

??????? me.btncel.size = new system.drawing.size (90, 25)

??????? me.btncel.tabindex = 8

??????? me.btncel.text = "& c cancel"

??????? '

??????? 'btnok

??????? '

??????? me.btnok.location = new system.drawing.point (72, 202)

??????? me.btnok.name = "btnok"

??????? me.btnok.size = new system.drawing.size (90, 25)

??????? me.btnok.tabindex = 7 ??????? me.btnok.text = "& o determined"

??????? '

??????? 'LBLSERVER

??????? '

??????? me.lblserver.location = new system.drawing.point (29, 30)

??????? me.lblserver.name = "lblserver"

??????? me.lblServer.size = new system.drawing.size (77, 26)

??????? me.lblserver.tabindex = 9

??????? me.lblserver.text = "server"

??????? me.lblserver.textalign = system.drawing.contentAlignment.middleright

??????? '

??????? 'frmmain

??????? '

??????? me.autoscalebasesize = new system.drawing.size (6, 14)

??????? me.clientsize = new system.drawing.size (336, 253)

??????? me.controls.add (me.groupbox1)

??????? me.name = "frmmain"

??????? me.groupbox1.resumeLayout (FALSE)

??????? me.grplaogin.resumeLayout (false)

??????? me.grbdatabase.resumeLayout (FALSE)

??????? me.ResumeLayout (false)

??? End Sub

#End region

?

??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ???? xmldoc = new xmldocument ??????? xmldoc.Load ("web.config") ??????? Topm = xmldoc.documentelement.childNodes ??????? for Each Element AS XMLELEMENT IN TOPM ??????????? if Element.name.tolower () = "Appsettings" Then ??????????????? DIM _Node As XMLNodelist = Element.childNodes ???????????????? IF _Node.count> 0 Then ??????????????????????????????????????????????????????? ????????????????????? f e e e... = "Servername" the _ ???????????? ??????????????? txtServerName.text = el.attributes ("value"). Value ??????????????????????????????????????? ?? if el.attributes ("key"). Innerxml = "DatabaseName" the _ ?????????????????? CBODATABASE.TEXT = El.attributes ("value"). value ????????????????????????????????? IF EL.ATTRIBUTES ("key"). InnerXML = "username" the _ ????????????????????????????? txtusername.text = securityService.symmetr Icdecrypt ("Value"). Value) ???????????????????????? IF EL.Attributes ("Key"). InnerXML = "Password "THEN _ ???????????????????????????? txtpassword.text = securityservice.symmetricDecrypt (El.attributes (" value "). Value)? ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? "WindowsAuthentication" Then _ ??????????? ??????????????? me.cbwindowsauthentication.Checked = convert.toboolean (El.attributes ("value"). Value) ?????????????? ????? next ?????????????????????????????? negative ??????? next

??? End Sub

??? '******************************************************* **************************************************************** ?? ? '** Class: ????????? getDatabases ???' ** ??? '** description: ??? ???' ************ *********************************************************** ************************************** ?????????????? Try

??????????? DIM ODATABASE AS New Database ??????????? CBODATABASE.BEGINUPDATE () ??????????? Cursor.current = Cursors.Waitcursor

??????????? configuration.server = txtServerName.text ??????????? configuration.Database = "master" 'cbodatabase.text ?????????? ? Configure.password = txtpassword.text ??????????? configuration.username = txtusername.text ??????????? configure.WindowsAuthentication = CBWindowsAuthentication.checked

??????????? DIM RD AS SQLCLIENT.SQLDATAREADER ??????????? rd = odatabase.getdatabases ??????????? while rd.read () ??????????????? CBODATABASE.ITEMS.ADD (rd.getstring (1)) ??????????? End while ????????? ?? rd.close () ??????????? CBODATABASE.SORTED = TRUE

??????? catch acumext as system.Runtime.InteropServices.comException

??????????? cbodatabase.datasource = Nothing ??????????? cbodatabase.items.clear () ??????????? cbodatabase.text = " "

??????????? msgbox ("could not connect to" & txtservername.text & ".", Msgboxstyle.exclamation, _ ??????????????? "SQL Connection Error ") ??????? Catch E1 AS EXCEPTION ??????????? MSGBOX (E1.MESSAGE)

??????? finally

??????????? CBODATABASE.Endupdate () ???????????? cursor.current = CURSORS.DEFAULT

??????? End Try

??? End Sub

????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????. ???? xmldoc = new xmldocument ??????? xmldoc.Load ("web.config") ??????? Topm = xmldoc.documentelement.childNodes ??????? for Each Element AS XMLELEMENT IN TOPM ??????????? if Element.name.tolower () = "Appsettings" Then ??????????????? DIM _Node As XMLNodelist = Element.childNodes ???????????????? IF _Node.count> 0 Then ??????????????????????????????????????????????????????? ????????????????????? f e e e.. = "Servername" the _ ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????? ?????????????? El.attributes ("value"). value = txtservername.text ?????????????????????????????????????????????????????????????????????????????????????? IF El.attributes ("key"). InnerXML = "DatabaseName" Then _ ?????????????????????????? El.attributes ("Value ") .Value = CBODATABASE.TEXT ???????????????????????? IF EL.ATTRIBUTES (" key "). InnerXML =" username "the _ ?? ???????????????????????? El.attributes ("value"). Value = SecurityService. Symmetricencrypt (txtusername.text) ????????????????? ?At u?es ("key"). InnerXml = "password" THEN _ ????? ?????????????????????? El.attributes ("value"). Value = securityService.symmetricencrypt (txtpassword.text) ?????????? ?????????????? IF EL.Attributes ("key"). Innerxml = "windowsauthentication" Then _ ?????????????????????????????????????????????????????????????????????????????????? ?????? el.attributes ("value"). value = me.cbwindowsauthentication.checked.toString () ???????????????? ??????????? End if ??????????? end if ??????? narix

??????? 'write XML to file ??????? Xmldoc.save ("Web.config") ??????? me.close () ??? End Sub

??????????????????????I

??????? if cbWindowsauthentication.checked the ??????????? txtusername.text = "" ??????????? txtpassword.text = "" ????? ??????? txtusername.enabled = false ??????????? txtpassword.Enabled = false ??????? Else ??????????? txtusername.enabled = True ??????????? txtpassword.enabled = True ??????? end if

??????? configuration.windowsauthentication = CBWINDOWSAUTHENTICATION.CHECKED? ?? End Sub

???????????. Byval e as system.Object, byval e as system.eventargs Handles btncel.click ??????? if msgbox ("Are you sure you want to eXIT W / o configuring the database? ", Msgboxstyle.okcancel] = msgboxresult.ok then ??????????? me.close () ??????? end if ??? end sub ??? private sub txtpassword_lostfocus (Byval Sender As system.object, byval e as system.eventargs Handles txtpassword.lostfocus ??????? if cbwindowsauthentication.checked or (txtusername.text <> "" and txtpassword.text <> ")") THEN ???? ??????? getDatabases () ??????? end if? ??? End Subend Class

The SecurityService class is used to add / decrypt so that users cannot see database users and passwords directly from Web.config.

转载请注明原文地址:https://www.9cbs.com/read-117400.html

New Post(0)