VB.net技巧论坛摘录(一)

  1. 在VB.NET中如何获得桌面的路径?
    ==========================================================================
    System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) 
  2. 更改桌面(API)
    ==========================================================================
        Public Const SPI_SETDESKWALLPAPER = &H14
        Public Const SPIF_SENDWININICHANGE = &H2
        Public Const SPIF_UPDATEINIFILE = &H1
        Public Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" (ByVal uAction As Integer, ByVal uParam As Integer, ByVal lpvParam As String, ByVal fuWinIni As Integer) As Integer

     Dim t As Integer = SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, "C:/DDDD/1.bmp", SPIF_UPDATEINIFILE)
  3. 进程相关
    ==========================================================================
    dim pcs() AS Process
    'Process1.GetProcessByName("QQ.exe").Kill()
    pcs= Process.GetProcessesByName("iExplore");
    Dim i as integer
    For i=0 to pcs.Length-1
      pcs(i).Kill
    Next
  4. 如何在VB 2005中获取本地IP地址
    ==========================================================================
    System.Net.Dns.GetHostEntry(My.Computer.Name).AddressList(0).ToString
    WINSOCK是VB6的一个控件,如果你想在VB2005里使用它,可以在添加引用的COM选项卡里找到它
  5. 如何启用用户帐号
    ==========================================================================
            '创建用户
            '"dc=lsw,dc=com"为我系统所在的域
            Dim ent As DirectoryEntry = New DirectoryEntry("LDAP://CN=users,dc=lsw,dc=com")
            Dim ou As DirectoryEntry = ent '.Children.Find("OU=Consulting")
            Dim usr As DirectoryEntry = ou.Children.Add("CN=New User", "user")
            usr.Properties("samAccountName").Value = "newuser"
            usr.CommitChanges()
  6. '启用账户
    ==========================================================================
            '"ActiveDs.ADS_USER_FLAG.ADS_UF_ACCOUNTDISABLE"我直接对照Msdn提供的表格,查到它的值为2
            Dim usr As DirectoryEntry = New DirectoryEntry("LDAP://CN=New User,CN=users,dc=lsw,dc=com")
            Dim val As Integer = CInt(usr.Properties("userAccountControl").Value)
            usr.Properties("userAccountControl").Value = val And Not 2
            usr.CommitChanges()
  7.         '禁用账户
    ==========================================================================
            Dim usr As DirectoryEntry = New DirectoryEntry("LDAP://CN=New User,CN=users,dc=lsw,dc=com")
            Dim val As Integer = CInt(usr.Properties("userAccountControl").Value)
            usr.Properties("userAccountControl").Value = val Or 2
            usr.CommitChanges() 
  8. 改变帐户密码    
    ==========================================================================
    1、获取用户名
        My.User.Name
    2、更改密码
    先引用并导入System.DirectoryServices命名空间
            Dim MyString As String = "WinNT://" + My.User.Name.Replace("/", "/")
            Dim MyDirectoryEntry As New DirectoryEntry(MyString)
            MyDirectoryEntry.Invoke("setPassword", "newpassword")
            MyDirectoryEntry.CommitChanges() 
    ==========================================================================
    网络流量数据、网络地址信息和本地计算机的地址更改通知的访问  
    ========================================================================== 
     
    System.Net.NetworkInformation 命名空间提供对网络流量数据、网络地址信息和本地计算机的地址更改通知的访问。该命名空间还包含实现 Ping 实用工具的类。可以使用 System.Net.NetworkInformation.Ping 和相关的类检查是否可通过网络连接到计算机。
    这个命名空间大部分功能是用于获取信息的
  9. 如何读写注册表?
    ==========================================================================
    '演示了如何判断某一键值是否存在(代码片段)
    '引用命名空间
    Imports Microsoft.Win32
       Public Function DEMO()
            Dim regCurrUser As RegistryKey = Registry.CurrentUser
            Dim exists As Boolean = True
            Dim path As String = "Software/Microsoft/Demo/2.0"
            If regCurrUser.OpenSubKey(path) Is Nothing Then
                exists = False
            End If
            Return exists
        End Function
    在VB2005里还可以使用My关键字(不用去引用Microsoft.Win32命名空间)

    '同样功能,VB2005的另一种实现方法
        Public Function DEMO()
            Dim exists As Boolean = True
            Dim path As String = "Software/Microsoft/Demo/2.0"
            If My.Computer.Registry.CurrentUser.OpenSubKey(path) Is Nothing Then
                exists = False
            End If
            Return exists
        End Function
  10. 编写随Windows启动自动运行的程序
    ==========================================================================
    方法一:通过注册表
    imports Microsoft.Win32.Registry
    Dim Reg As Microsoft.Win32.RegistryKey
        Reg = CurrentUser.OpenSubKey("Software/Microsoft/Windows/CurrentVersion/Run", True)
        Reg.SetValue("MengXianHui", Application.ExecutablePath)
  11. 编写带参数的程序
    ==========================================================================
        Private Shared _comm() As String = Nothing
    Public Shared Sub Main(ByVal args() As String)
            _comm = args
            Application.Run(New Form1)
        End Sub

    Private Sub mybase_load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
            For i As Integer = 0 To _comm.Length - 1
                If _comm(i) = "Hello,World" Then
                    Me.Text = "Hello,World"
                End If
                If _comm(i) = "Hello" Then
                    MessageBox.Show("Hello")
                End If
            Next
        End Sub
    建立lnk C:/Project/WindowsApplication1/bin/WindowsApplication1.exe  Hello,World


    System.Environment.GetCommandLineArgs方法,

    Process p = Process.GetCurrentProcess();
    ProcessStartInfo ps = p.StartInfo;
    string str  = ps.Arguments;
  12. vb.net2005读服务器中的XML文件方法?
    ==========================================================================
    Dim myUrl As String = "http://www.XXX.com/Updater/ServerVer.xml"
    Dim reader As New XmlTextReader(myUrl)
    Dim Ver, DownLoadsFLieCoust, DownLoadsFLie As String
    While reader.Read
      If reader.NodeType = XmlNodeType.Element Then
         Select Case reader.Name
              Case "Ver"
                 reader.Read()
                 Ver = reader.Value
              Case "DownLoadsFLieCoust"
                 reader.Read()
                 DownLoadsFLieCoust = reader.Value
             Case "DownLoadsFLie"
                 reader.Read()
                 DownLoadsFLie = reader.Value
           End Select
      End If
    End While
  13. VB.NET如何制作不规则窗体...如圆形窗体
    ==========================================================================
    Imports System.Drawing.Drawing2D
    Public Class Form1
        Private Declare Function SetWindowRgn Lib "user32" (ByVal hWnd As Long, ByVal hRgn As Long, ByVal bRedraw As Boolean) As Long
        Private Declare Function CreateEllipticRgn Lib "gdi32" (ByVal X1 As Long, ByVal Y1 As Long, ByVal X2 As Long, ByVal Y2 As Long) As Long
        Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long

     Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Me.Width = 300
            Me.Height = 220
            Me.BackColor = Color.RoyalBlue
            Dim m_path As GraphicsPath
            m_path = New GraphicsPath(FillMode.Winding)
            m_path.AddEllipse(1, 1, 200, 200)
            Dim m_region As New Region(m_path)
            Me.Region = m_region
        End Sub
    End Class
  14. VB.NET如何动态添加组件及相关事件
    ==========================================================================
    将Mybt(i,j)定义成全局的数组变量,i,j也是全局变量
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            y = 6
            For i = 0 To 16
                x = 0
                For j = 0 To 7
                    Mybt(i, j) = New Button
                    Mybt(i, j).Location = New Point(x, y)
                    Mybt(i, j).Size = New System.Drawing.Size(27, 27) '按钮大小为(27,27)
                    Mybt(i, j).Text = i & "," & j
                    Me.Controls.Add(Mybt(i, j))
                    Me.ToolTip1.SetToolTip(Mybt(i, j), j & "," & i) 'ToolTip1已在窗体设计器中加              
                    x += 27
                    AddHandler Mybt(i, j).Click, AddressOf a '事件委托
                Next j
                y += 27
            Next i
     End Sub

    Private Sub a(ByVal sender As Object, ByVal e As System.EventArgs)   
          MessageBox.Show(CType(sender, Button).Text)
    End Sub

  15. 如何实现遍历指定目录内的文件?
    ==========================================================================
     Dim files As ReadOnlyCollection(Of String)
            files = My.Computer.FileSystem.GetFiles("C:/", FileIO.SearchOption.SearchTopLevelOnly, "*.*")        Dim FileName(files.Count) As String
            Dim i As Integer
            For i = 0 To files.Count - 1
                FileName(i) = files(i).ToString
            Next
  16. 请问在vb2005里怎样才能提取到应用程序的图标?
    ==========================================================================
    Dim ico As Icon = Drawing.Icon.ExtractAssociatedIcon("E:/FunPromRM.exe")
  17. 请问在vb2005里怎样实现关机 、注消、重启
    ==========================================================================
    1.shutdown.bat(关机)
    rundll32.exe.Exe shell32.dllSHExitWindowsEx 1
    2.logoff.bat(注销)
    rundll32.exe.Exe shell32.dllSHExitWindowsEx 0
    3.reboot.bat(重启)
    rundll32.exe.Exe shell32.dllSHExitWindowsEx 2
    4.lock.bat(锁定桌面)
    rundll32.exe.Exe user32.dllLockWorkStation
    用Shell语句执行:
  18. 我想问一下在用户网络断开后,程序显示离线的功能怎么实现
    ==========================================================================
    请参考一下NetworkChange.NetworkAvailabilityChanged 事件
    My.Computer.Network.IsAvailable属性会返回一个布尔值来表示电脑是否已连接至网络. My.Computer.Network.Ping方法能够去验证与制定远端主机的连线.
    例如:
    If My.Computer.Network.Ping("192.168.0.1") then
    Messagebox.Show("连接成功.")
    Else
    Messagebox.Show("连接失败.")
    End If

你可能感兴趣的:(VB.net技巧论坛摘录(一))