Windows窗体应用程序中的线程处理-VB.Net

[vb]
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click 
    Dim r As Boolean = GetUserSID(“MyADServer”, “username”,
“password”).Length > 0 
    MsgBox(r) 
End Sub 
 
Public Shared Function GetUserSID(ByVal ldapServer As String, ByVal
userName As String, ByVal password As String) As String 
    Dim strPath As String 
    If ldapServer.IndexOf(“.”c) <> -1 Then 
        strPath = String.Format(“LDAP://{0}”, ldapServer) 
    Else 
        strPath = String.Format(“WinNT://{0}/{1}, user”, ldapServer,
userName) 
    End If 
    Dim entry As New DirectoryEntry(strPath, userName, password) 
    Try 
        Return New
SecurityIdentifier(entry.Properties(“objectSid”).Value, 0).Value 
    Catch 
        Return “” 
    Finally 
        entry.Dispose() 
    End Try 
End Function 

vb.net调用webService源码,vb.netwebservice

[csharp]  Imports System.CodeDom 
Imports System.CodeDom.Compiler 
Imports System.Security.Permissions 
Imports System.Web.Services.Description 
Imports System.Reflection 
 
Public Class DynamicWebService 
    Public Function CallWebService(ByVal webServiceAsmxUrl As String,

       ByVal serviceName As String, ByVal methodName As String, _ 
       ByVal args() As Object) As Object 
 
        Try 
            Dim client As System.Net.WebClient = New
System.Net.WebClient() 
 
            ‘-Connect To the web service 
            Dim stream As System.IO.Stream = _ 
                client.OpenRead(webServiceAsmxUrl + “?wsdl”) 
 
            ‘Read the WSDL file describing a service. 
            Dim description As ServiceDescription =
ServiceDescription.Read(stream) 
 
            ‘LOAD THE DOM”””””””””””””’ 
 
            ‘–Initialize a service description importer. 
            Dim importer As ServiceDescriptionImporter = New
ServiceDescriptionImporter() 
            importer.ProtocolName = “Soap12” ‘ Use SOAP 1.2. 
            importer.AddServiceDescription(description, Nothing,
Nothing) 
 
            ‘–Generate a proxy client.  
 
            importer.Style = ServiceDescriptionImportStyle.Client 
            ‘–Generate properties to represent primitive values. 
            importer.CodeGenerationOptions = _ 
                
System.Xml.Serialization.CodeGenerationOptions.GenerateProperties 
 
            ‘Initialize a Code-DOM tree into which we will import the
service. 
            Dim nmspace As CodeNamespace = New CodeNamespace() 
            Dim unit1 As CodeCompileUnit = New CodeCompileUnit() 
            unit1.Namespaces.Add(nmspace) 
 
            ‘Import the service into the Code-DOM tree.  
            ‘This creates proxy code that uses the service. 
 
            Dim warning As ServiceDescriptionImportWarnings = _ 
                           importer.Import(nmspace, unit1) 
 
            If warning = 0 Then 
 
                ‘–Generate the proxy code 
                Dim provider1 As CodeDomProvider = _ 
                          CodeDomProvider.CreateProvider(“VB”) 
                ‘–Compile the assembly proxy with the // appropriate
references  
                Dim assemblyReferences() As String 
                assemblyReferences = New String() {“System.dll”, _ 
                    “System.Web.Services.dll”, “System.Web.dll”, _ 
                    “System.Xml.dll”, “System.Data.dll”} 
                Dim parms As CompilerParameters = New
CompilerParameters(assemblyReferences) 
                parms.GenerateInMemory = True ‘(Thanks for this line
nikolas) 
                Dim results As CompilerResults =
provider1.CompileAssemblyFromDom(parms, unit1) 
 
                ‘-Check For Errors 
                If results.Errors.Count > 0 Then 
 
                    Dim oops As CompilerError 
                    For Each oops In results.Errors 
                       
System.Diagnostics.Debug.WriteLine(“========Compiler
error============”) 
                       
System.Diagnostics.Debug.WriteLine(oops.ErrorText) 
                    Next 
                    Throw New System.Exception(“Compile Error Occurred
calling webservice.”) 
                End If 
 
                ‘–Finally, Invoke the web service method 
                Dim wsvcClass As Object =
results.CompiledAssembly.CreateInstance(serviceName) 
                Dim mi As MethodInfo =
wsvcClass.GetType().GetMethod(methodName) 
                Return mi.Invoke(wsvcClass, args) 
 
            Else 
                Return Nothing 
            End If 
 
        Catch ex As Exception 
            Throw ex 
        End Try 
    End Function 
End Class 

Imports System.CodeDom
Imports System.CodeDom.Compiler
Imports System.Security.Permissions
Imports System.Web.Services.Description
Imports System.Reflection

Public Class DynamicWebService
    Public Function CallWebService(ByVal webServiceAsmxUrl As String,
_
       ByVal serviceName As String, ByVal methodName As String, _
       ByVal args() As Object) As Object

        Try
            Dim client As System.Net.WebClient = New
System.Net.WebClient()

            ‘-Connect To the web service
            Dim stream As System.IO.Stream = _
                client.OpenRead(webServiceAsmxUrl + “?wsdl”)

            ‘Read the WSDL file describing a service.
            Dim description As ServiceDescription =
ServiceDescription.Read(stream)

            ‘LOAD THE DOM”””””””””””””’

            ‘–Initialize a service description importer.
            Dim importer As ServiceDescriptionImporter = New
ServiceDescriptionImporter()
            importer.ProtocolName = “Soap12” ‘ Use SOAP 1.2.
            importer.AddServiceDescription(description, Nothing,
Nothing)

            ‘–Generate a proxy client.

            importer.Style = ServiceDescriptionImportStyle.Client
            ‘–Generate properties to represent primitive values.
            importer.CodeGenerationOptions = _
                
System.Xml.Serialization.CodeGenerationOptions.GenerateProperties

            ‘Initialize a Code-DOM tree into which we will import the
service.
            Dim nmspace As CodeNamespace = New CodeNamespace()
            Dim unit1 As CodeCompileUnit = New CodeCompileUnit()
            unit1.Namespaces.Add(nmspace)

            ‘Import the service into the Code-DOM tree.
            ‘This creates proxy code that uses the service.

            Dim warning As ServiceDescriptionImportWarnings = _
                           importer.Import(nmspace, unit1)

            If warning = 0 Then

                ‘–Generate the proxy code
                Dim provider1 As CodeDomProvider = _
                          CodeDomProvider.CreateProvider(“VB”)
                ‘–Compile the assembly proxy with the // appropriate
references
                Dim assemblyReferences() As String
                assemblyReferences = New String() {“System.dll”, _
                    “System.Web.Services.dll”, “System.Web.dll”, _
                    “System.Xml.dll”, “System.Data.dll”}
                Dim parms As CompilerParameters = New
CompilerParameters(assemblyReferences)
                parms.GenerateInMemory = True ‘(Thanks for this line
nikolas)
                Dim results As CompilerResults =
provider1.CompileAssemblyFromDom(parms, unit1)

                ‘-Check For Errors
                If results.Errors.Count > 0 Then

                    Dim oops As CompilerError
                    For Each oops In results.Errors
                       
System.Diagnostics.Debug.WriteLine(“========Compiler
error============”)
                       
System.Diagnostics.Debug.WriteLine(oops.ErrorText)
                    Next
                    Throw New System.Exception(“Compile Error Occurred
calling webservice.”)
                End If

                ‘–Finally, Invoke the web service method
                Dim wsvcClass As Object =
results.CompiledAssembly.CreateInstance(serviceName)
                Dim mi As MethodInfo =
wsvcClass.GetType().GetMethod(methodName)
                Return mi.Invoke(wsvcClass, args)

            Else
                Return Nothing
            End If

        Catch ex As Exception
            Throw ex
        End Try
    End Function
End Class

调用:

[csharp]  Imports System.Net 
Public Class Form1    
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click 
        Dim WebserviceUrl As String =
“” 
 
        ‘specify service name 
        Dim serviceName As String = “EsbBusService” 
 
        ‘specify method name to be called 
        Dim methodName As String = “Process” 
 
        ‘Arguments passed to the method 
 
 
        Dim objCallWS As New DynamicWebService 
        Dim sSessionID = objCallWS.CallWebService(WebserviceUrl,
serviceName, methodName, {txtInput.Text}) 
        TextBox1.Text = sSessionID 
 
    End Sub 
End Class 

Imports System.Net
Public Class Form1  
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
        Dim WebserviceUrl As String =
“”

        ‘specify service name
        Dim serviceName As String = “EsbBusService”

        ‘specify method name to be called
        Dim methodName As String = “Process”

        ‘Arguments passed to the method

        Dim objCallWS As New DynamicWebService
        Dim sSessionID = objCallWS.CallWebService(WebserviceUrl,
serviceName, methodName, {txtInput.Text})
        TextBox1.Text = sSessionID

    End Sub
End Class

 

[csharp] Imports System.CodeDom Imports System.CodeDom.Compiler
Imports System.Security.Permissions Imports
System.Web.Services.Desc…

1,读取XML 
Imports System.Xml
Imports System.Web.HttpContext
  Public Sub getPro()
        Dim FilePath As String =
Current.Server.MapPath(“define_auth.xml”)
        Dim re As XmlTextReader = New XmlTextReader(FilePath)
        Dim name As String
        Try
            While re.Read
                Select Case re.NodeType
                    Case XmlNodeType.Element
                        name = re.Name
                    Case XmlNodeType.Text
                        setProperty(name, re.Value)
                End Select
            End While
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    ‘name      :XML Node的名字
    ‘Nodetext  :XML Node的值
    Private Sub setProperty(ByVal NodeName As String, ByVal NodeValue As
String)
        Select Case NodeName
            Case “provider”
                sqlProvider = NodeValue
            Case “connectionstring”
                connectionStr = NodeValue
        End Select
    End Sub

   Private Sub BackgroundWorker1_ProgressChanged(ByVal sender As Object, ByVal e As ProgressChangedEventArgs) _
      Handles BackgroundWorker1.ProgressChanged
      ‘ Reject nested calls.
      If callInProgress Then Return
      callInProgress = True
      ‘ Display the message, received in the UserState property.
      Me.lblMessage.Text = e.UserState.ToString()
      ‘ Display all files added since last call
      For i As Integer = lstFiles.Items.Count To files.Count – 1
         lstFiles.Items.Add(files(i))
      Next
      Me.Refresh()
      ‘ Let Windows process message in the queue. 
      ‘ If you omit this call, clicks on buttons are ignored.
      Application.DoEvents()
      callInProgress = False
   End Sub

摘自 Modest的专栏

    Private Sub Button8_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button8.Click
        Dim myFile As System.IO.StreamReader = _
        New System.IO.StreamReader(Server.MapPath(“data.xml”),
System.Text.Encoding.Default)
        ‘Attention System.Text.Encoding.Default
        Dim myString As String = myFile.ReadToEnd() ‘myString was read
        myFile.Close()
    End Sub

      ‘ Read the files in this folder and all subfolders.
      Dim files As New List(Of String)
      For Each fi As String In Directory.GetFiles(path)
         files.Add(fi)
      Next
      For Each di As String In Directory.GetDirectories(path)
         files.AddRange(GetFiles(di))
      Next
      Return files
   End Function

] Imports System.DirectoryServices Imports
System.Security.Principal [vb] Private Sub Button1_Click(ByVal sender
As System.Object, ByVal e As System.EventArgs) Handles
Button1.C…

    Private Sub Button7_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button7.Click
        Dim xmlDoc As XmlDocument = New XmlDocument
        xmlDoc.Load(Server.MapPath(“data.xml”))
        Dim root As XmlNode = xmlDoc.SelectSingleNode(“Employees”)
        Dim xnl As XmlNodeList =
xmlDoc.SelectSingleNode(“Employees”).ChildNodes
        Dim i As Integer
        For i = 0 To i < xnl.Count
            Dim xe As XmlElement
            xe = xnl.Item( i )
            If xe.GetAttribute(“genre”) = “張さん” Then
                root.RemoveChild(xe)
                If i < xnl.Count Then i = i – 1
            End If
        Next
        xmlDoc.Save(Server.MapPath(“data.xml”))
    End Sub

图片 1图片 2代码

[vb]
Imports System.DirectoryServices 
Imports System.Security.Principal 

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
        Dim xmlDoc As XmlDocument = New XmlDocument
        xmlDoc.Load(Server.MapPath(“data.xml”))
        Dim root As XmlNode = xmlDoc.SelectSingleNode(“Employees”)
        Dim xe1 As XmlElement = xmlDoc.CreateElement(“Node”)
        xe1.SetAttribute(“genre”, “張さん”)
        xe1.SetAttribute(“ISBN”, “1-1111-1”)
        Dim xesub1 As XmlElement = xmlDoc.CreateElement(“title”)
        xesub1.InnerText = “New Book”
        xe1.AppendChild(xesub1)
        Dim xesub2 As XmlElement = xmlDoc.CreateElement(“author”)
        xesub2.InnerText = “H”
        xe1.AppendChild(xesub2)
        Dim xesub3 As XmlElement = xmlDoc.CreateElement(“price”)
        xesub3.InnerText = “158.3”
        xe1.AppendChild(xesub3)
        root.AppendChild(xe1)
        xmlDoc.Save(Server.MapPath(“data.xml”))

   ‘ (This method runs in a non-UI thread.)
   Sub SearchFiles(ByVal arg As Object)
      ‘ Retrieve the argument.
      Dim path As String = arg.ToString()
      ‘ Prepare the delegate
      threadSafeDelegate = New ShowMessageDelegate(AddressOf ShowMessage)
      ‘ Invoke the worker procedure. (The result isn’t used in this demo.) 
      Dim files As List(Of String) = GetFiles(path)
      ‘ Show that execution has terminated.
      Dim msg As String = String.Format(“Found {0} files”, files.Count)
      Me.Invoke(threadSafeDelegate, msg)
   End Sub

    Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button6.Click
        Dim xmldoc As XmlDocument = New XmlDocument
        xmldoc.Load(Server.MapPath(“data.xml”))
        Dim xnl As XmlNodeList =
xmldoc.SelectSingleNode(“Employees”).ChildNodes
        For Each xn As XmlNode In xnl
            Dim xe As XmlElement
            xe = xn
            xe.RemoveAttribute(“genre”)
            Dim nls As XmlNodeList = xe.ChildNodes
            For Each xn1 As XmlNode In nls
                Dim xe2 As XmlElement
                xe2 = xn1
                If xe2.Name = “flag” Then
                    xe.RemoveChild(xe2)
                End If
            Next
        Next
        xmldoc.Save(Server.MapPath(“data.xml”))
    End Sub

   ‘ We need this variable to avoid nested calls to ProgressChanged.
   Dim callInProgress As Boolean

                Dim nls As XmlNodeList = xe.ChildNodes
                For Each xn1 As XmlNode In nls
                    Dim xe2 As XmlElement
                    xe2 = xn1
                    ‘xe2 =(XmlElement)xn1
                    If xe2.Name = “author” Then
                        xe2.InnerText = “量”
                    End If
                Next
            End If
        Next
        xmldoc.Save(Server.MapPath(“data.xml”))
    End Sub

   ‘ A recursive function that retrieves all the files in a directory tree.
   ‘ (This method runs in a non-UI thread.)
   Function GetFiles(ByVal path As String) As List(Of String)
      ‘ Display a message.
      Dim msg As String = String.Format(“Parsing directory {0}”, path)
      Me.Invoke(threadSafeDelegate, msg)

发表评论

电子邮件地址不会被公开。 必填项已用*标注