Related Posts Plugin for WordPress, Blogger...

2014年8月29日 星期五

VB.Net Snippet程式碼片段-Windows系統-記錄,處理序,登錄,服務

Visual Studio2013的程式碼片段Snippet,
包含了許多的項目,
本文介的項目為Windows系統-記錄,處理序,登錄,服務

12個項目列示如下,
1.ASP.NET MVC4
2.My Code程式碼片段
3.Office程式碼片段
4.Windows Form應用程式
5.Windows系統-記錄,處理序,登錄,服務
6.WPF
7.其他-連接,安全性,工作流程
8.基本-集合,資料類型,檔案系統,數學
9.應用程式-編譯,資源和設定
10.測試
11.程式碼模式-If,For Each,Try Catch,Property
12.資料-LINQ,XML,設計工具,ADO.NET



依照Visusl Studio2013程式片段管理員中,
列示的項目順序,
把提供的Snippet詳細內容列示在下方,
雖然跟使用Snippet右鍵時,
列示的項目順序不同,
不過可以先查閱一下內容



5.Windows系統-記錄,處理序,登錄,服務
5.0.1回應計時器事件: 回應計時器事件。
' Start the timer with the given interval
    Private Sub StartTimer()
        Dim Timer1 As New Timer()
        AddHandler Timer1.Tick, AddressOf Timer1_Tick
        Timer1.Interval = 1000
        Timer1.Start()
    End Sub
    Private Sub Timer1_Tick(ByVal obj As Object, ByVal e As EventArgs)

    End Sub

5.0.2在控制項上使用 BeginInvoke 以非同步執行程式碼: 在控制項上使用 BeginInvoke 以非同步執行程式碼。
Delegate Sub InvokeDelegate()
    Public Sub Begin_Invoke()
        TextBox1.BeginInvoke(New InvokeDelegate(AddressOf InvokeMethod))
    End Sub
    Public Sub InvokeMethod()
        TextBox1.Text = "Invoked"
    End Sub

5.0.3系統電源模式變更時執行動作: 系統電源模式變更時執行動作。
Dim WithEvents systemEvent As SystemEvents
    Private Sub systemEvent_PowerModeChanged(ByVal sender As Object, ByVal e As Microsoft.Win32.PowerModeChangedEventArgs) Handles systemEvent.PowerModeChanged
        Select Case e.Mode
            Case PowerModes.Resume

            Case PowerModes.StatusChange

            Case PowerModes.Suspend

            Case Else

        End Select
    End Sub

5.0.4尋找目前使用者的名稱: 擷取目前使用者的名稱
Dim userName = My.User.Name

5.1Windows-系統資訊
5.1.1列出環境變數: 建立列出所有使用者環境變數及其值的字串。
Dim environmentVariables = Environment.GetEnvironmentVariables(EnvironmentVariableTarget.User)

5.1.2回應Windows使用者偏好設定的變更: 示範如何處理對 Windows 使用者偏好設定所做的變更
Dim WithEvents systemEvent As SystemEvents
    Private Sub systemEvent_UserPreferenceChanged(ByVal sender As System.Object, ByVal e As UserPreferenceChangedEventArgs) Handles systemEvent.UserPreferenceChanged
        Select Case e.Category
            Case UserPreferenceCategory.Accessibility

            Case UserPreferenceCategory.Color

            Case UserPreferenceCategory.Desktop

            Case UserPreferenceCategory.General

            Case UserPreferenceCategory.Icon

            Case UserPreferenceCategory.Keyboard

            Case UserPreferenceCategory.Locale

            Case UserPreferenceCategory.Menu

            Case UserPreferenceCategory.Mouse

            Case UserPreferenceCategory.Policy

            Case UserPreferenceCategory.Power

            Case UserPreferenceCategory.Screensaver

            Case UserPreferenceCategory.VisualStyle

            Case UserPreferenceCategory.Window
            Case Else
        End Select
    End Sub

5.1.3判斷Windows系統目錄: 指派系統目錄給字串。
Dim systemDirectory = System.Environment.SystemDirectory

5.1.4判斷可用的系統記億體量: 判斷目前可用的系統記憶體量。
Dim totalPhysicalMemmory = My.Computer.Info.TotalPhysicalMemory

5.1.5判斷目前的Windows版本: 判斷正在使用的 Windows 版本
Dim osVersion = My.Computer.Info.OSVersion

5.1.6判斷桌面的顯示解析度: 判斷桌面的寬度和長度,以像素為單位。
Dim height = My.Computer.Screen.Bounds.Height
Dim width = My.Computer.Screen.Bounds.Width

5.1.7建立環境變數: 建立使用者環境變數以保存程式之間的執行狀態。
Environment.SetEnvironmentVariable("Variable", "Value", EnvironmentVariableTarget.User)

5.1.8尋找可用的磁碟空間量: 傳回指定磁碟上可用的磁碟空間量。
Dim drive = My.Computer.FileSystem.GetDriveInfo("C:\")
Dim space = drive.AvailableFreeSpace

5.1.9擷取功能表的使用者偏好設定: 擷取數項功能表設定。
Dim barSize = SystemInformation.MenuBarButtonSize
Dim buttonSize = SystemInformation.MenuButtonSize
Dim checkSize = SystemInformation.MenuCheckSize
Dim menuFont = SystemInformation.MenuFont
Dim height = SystemInformation.MenuHeight
Dim delay = SystemInformation.MenuShowDelay

5.1.10擷取本機電腦上目前的時間: 顯示本機電腦上目前的時間。
Dim localTime = My.Computer.Clock.LocalTime

5.1.11擷取地區設定: 擷取各種地區設定。
Dim installed = My.Computer.Info.InstalledUICulture
Dim current = My.Application.Culture
Dim ui = My.Application.UICulture

5.1.12擷取系統色彩的使用者偏好設定: 僅擷取多種系統色彩設定中的兩種。
Dim control = SystemColors.Control
Dim window = SystemColors.Window

5.1.13擷取協助工具偏好設定: 取得指出使用者是否選擇以高對比模式執行的值。
Dim contrastOn = SystemInformation.HighContrast

5.1.14擷取滑鼠設定: 擷取數項滑鼠設定。
Dim swapped = My.Computer.Mouse.ButtonsSwapped
Dim hasWheel = My.Computer.Mouse.WheelExists
Dim scrollLines = My.Computer.Mouse.WheelScrollLines
Dim buttons = SystemInformation.MouseButtons
Dim hoverSize = SystemInformation.MouseHoverSize
Dim hoverTime = SystemInformation.MouseHoverTime
Dim speed = SystemInformation.MouseSpeed
Dim delta = SystemInformation.MouseWheelScrollDelta
Dim nativeWheel = SystemInformation.NativeMouseWheelSupport

5.1.15擷取電池使用者壽命的相關資訊: 擷取電池剩餘使用壽命的百分比,並且在低於 5 % 時警告使用者。
Dim power = SystemInformation.PowerStatus
Dim percent = power.BatteryLifePercent
If percent < 0.05 Then
   MsgBox("Percent battery life remaining: " & percent * 100)
End If

5.1.16擷取圖示的使用者偏好設定: 擷取圖示顯示屬性的使用者偏好設定。
Dim size = SystemInformation.IconSize
Dim spacingSize = SystemInformation.IconSpacingSize
Dim horizontalSpace = SystemInformation.IconHorizontalSpacing
Dim verticalSpace = SystemInformation.IconVerticalSpacing

5.1.17擷取環境變數: 取環境變數的值。
Dim value = My.Application.GetEnvironmentVariable("Variable")

5.1.18 擷取鍵盤設定的使用者偏好設定: 擷取使用者的鍵盤設定。
Dim delay = SystemInformation.KeyboardDelay
Dim speed = SystemInformation.KeyboardSpeed

5.1.19變更地區設定: 變更地區設定。
My.Application.ChangeCulture("en-us")
My.Application.ChangeUICulture("en-us")


5.2Windows-事件記錄檔
5.2.1由事件記錄檔讀取特定應用程式建立的項目: 在清單中填入應用程式的所有事件記錄檔項目。
Dim log As New EventLog("Application")
Dim entries As New List(Of EventLogEntry)
For Each entry As EventLogEntry In log.Entries
    If entry.Source = "SourceName" Then
        entries.Add(entry)
    End If
Next

5.2.2由事件記錄檔讀取項目: 讀取所有事件記錄檔項目。
Dim log As New EventLog("Application")
Dim entries As EventLogEntryCollection = log.Entries

5.2.3由指定的來源寫入應用程式事件記錄檔: 將訊息寫入應用程式事件記錄檔。
Dim log As New EventLog("Application", "Machine Name", "SourceName")
log.WriteEntry("Message text", EventLogEntryType.Warning, 123)

5.2.4寫入My.Application記錄檔: 將字串寫入本機電腦上的應用程式事件記錄檔。
My.Application.Log.WriteEntry("An entry to the Application event log.")


5.3Windows-服務
5.3.1停止Windows服務: 在清單中填入應用程式的所有事件記錄檔項目。
Dim controller As New ServiceController("IISAdmin")
controller.Stop()

5.3.2啟動Windows服務: 啟動 Windows 服務應用程式。
Dim controller As New ServiceController("IISAdmiin")
controller.Start()

5.3.3暫停Windows服務: 使用 ServiceController 元件暫停本機電腦上的服務。
Dim controller As New ServiceController("IISAdmin")
controller.Pause()

5.3.4擷取服務清單: 建立本機電腦上服務的陣列字串。
Dim services() As ServiceController
services = ServiceController.GetServices()

5.3.5繼續執行Windows服務: 使用 ServiceController 元件繼續執行暫停的服務。
Dim controller As New ServiceController("IISAdmin")
If controller.Status = ServiceControllerStatus.Paused Then
    controller.Continue()
End If


5.4Windows-訊息佇列
5.4.1以程式設計的方式傳送和接收訊息: 傳送訊息至本機電腦上的佇列、接收該訊息,以及將主體格式化為字串。
Dim queue As New System.Messaging.MessageQueue(".\QueueName")
    ' Send a message to the queue.
queue.Send("Message text")
    ' Wait for the message to arrive, and then remove
    ' it from the queue.
    Dim msg As Message
msg = queue.Receive(New TimeSpan(0, 0, 3))
    ' Convert the body to a string.
msg.Formatter = New XmlMessageFormatter(New Type() {GetType(String)})
    Dim text As String = msg.Body.ToString


5.4.2建立公用訊息佇列: 在本機電腦上建立公用訊息佇列。
' Public queue
MessageQueue.Create(".\NewQueue")

5.4.3建立私用訊息佇列: 在本機電腦上建立私用訊息佇列。
' Private queue
MessageQueue.Create(".\Private$\PrivateQueue")

5.4.4指定已擷取訊息的格式子: 決定還原序列化訊息主體的方式。
Dim msg As New Message()
msg.Formatter = _
New XmlMessageFormatter(New Type() {GetType(String)})

5.4.5清除佇列內容: 使用 Purge 方法清除訊息佇列系統中,任何您可存取之佇列的內容。
Dim queue As New MessageQueue(".\QueueName")
queue.Purge()

5.4.6傳送訊息至訊息佇列: 傳送文字訊息至本機電腦上的佇列。
Dim queue As New MessageQueue(".\QueueName")
queue.Send("Message text", "Important message!")

5.4.7窺視訊息: 使用 Peek 方法查看佇列上的第一則訊息,而不由佇列中移除該訊息。
Dim queue As New MessageQueue(".\QueueName")
Dim firstMessage As Message
firstMessage = queue.Peek
firstMessage.Formatter = New XmlMessageFormatter(New Type() {GetType(String)})
Dim text As String = firstMessage.Body.ToString

5.4.8擷取本機電腦上的訊息佇列清單: 擷取本機電腦上的訊息佇列。
Dim queues() As MessageQueue
queues = System.Messaging.MessageQueue.GetPublicQueuesByMachine(".")

5.4.9擷取訊息標記: 擷取訊息陣列並建立訊息標記的陣列。
Dim queue As New MessageQueue(".\QueueName")
Dim msg() As Message
msg = queue.GetAllMessages()
Dim labels(msg.Length - 1) As String
For index As Integer = 0 To msg.Length - 1
    labels(index) = msg(index).Label
Next


5.5Windows-處理序
5.5.1列出應用程式: 建立包含執行中應用程式處理序清單的字串。
Dim processList() As Process
processList = Process.GetProcesses

5.5.2在預設瀏覽器中開啟Web網頁: 在預設瀏覽器中開啟 Web 網頁。
Process.Start("about:blank")

5.5.3停止應用程式: 停止執行中的應用程式。
Dim processList() As Process
processList = Process.GetProcessesByName("notepad")       
For Each proc As Process In processList
    If MsgBox("Terminate " & proc.ProcessName & "?", MsgBoxStyle.YesNo, "Terminate?") = MsgBoxResult.Yes Then
                proc.Kill()
            End If
    Next

5.5.4啟動應用程式: 啟動「記事本」應用程式。
Process.Start("notepad.exe")

5.5.5執行與檔案類型相關聯的程式: 執行與檔案類型相關聯的程式。
Process.Start("C:\Test.txt")


5.6Windows-登錄
5.6.1判斷登錄機碼是否存在: 查看指定的登錄機碼是否存在。
Dim exists As Boolean = False
Try
    If My.Computer.Registry.CurrentUser.OpenSubKey( _     
    "Software\Microsoft\TestApp\1.0") IsNot Nothing Then
        exists = True
    End If
Finally
    My.Computer.Registry.CurrentUser.Close()
End Try

5.6.2刪除登錄機碼: 刪除登錄機碼。
Using key As RegistryKey = My.Computer.Registry.LocalMachine.OpenSubKey("Software")
    key.DeleteSubKey("KeyToDelete")
End Using

5.6.3建立登錄機: 建立登錄機碼。
Dim newKey As RegistryKey newKey = _   My.Computer.Registry.CurrentUser.CreateSubKey( _ "Software\CompanyName\ProductName")

5.6.4撰寫登錄值: 將字串登錄值寫入目前的使用者登錄區。
My.Computer.Registry.SetValue( _ "HKEY_CURRENT_USER\Software\CompanyName\ProductName\KeyName", "Name", "value")

5.6.5讀取登錄值: 由目前的使用者登錄區讀取字串登錄值。
Dim keyValue As Object
keyValue = My.Computer.Registry.GetValue( "HKEY_CURRENT_USER\Software\CompanyName\ProductName\KeyName", "valueName", "Default Value")



相關貼文
VB.Net開發-使用Visual Studio2013提供之Snippet程式碼片段
VB.Net Snippet,利用工具軟體Snippet Editor,自訂常用的程式碼片段

沒有留言:

張貼留言

頁次