善用My命名空間快速完成常用工作 文/沈炳宏 ==========程式========= 程式1 [DllImport("winmm.dll", SetLastError=true, CallingConvention=CallingConvention.Winapi)] static extern bool PlaySound(string pszSound, IntPtr hMod,SoundFlags sf ); [Flags] public enum SoundFlags : int { SND_SYNC = 0x0000, … SND_RESOURCE = 0x00040004 } private void buttonPlay_Click( object sender, System.EventArgs e) { int err = 0; try { PlaySound("c:\windows\media\chimes.wav", IntPtr.Zero, SoundFlags.SND_FILENAME | SoundFlags.SND_ASYNC )) } catch { err = Marshal.GetLastWin32Error(); if (err != 0) MessageBox.Show( this, "Error " + err.ToString()); } } ==========程式========= ==========程式========= 程式2 My.Computer.Audio.Play("c:\windows\media\chimes.wav") ==========程式========= ==========程式========= 程式3 Public Sub ExceptionLogTest (ByVal fileName As String) Try Dim x As Object MsgBox(x.ToString) Catch ex As Exception My.Application.Log.WriteException(ex, _ TraceEventType.Error, _ fileName & "發生錯誤.") End Try End Sub ==========程式========= ==========程式========= 程式4 If My.Computer.Network.Ping("www.runpc.com.tw") = True Then MsgBox("電腦可以連接.") My.Computer.Network.DownloadFile _ ("http://www.runpc.com.tw/download/ 200611/ R154P191.txt ", _ "C:\R154P191.txt", _ "", "", False, 5000, True) Else MsgBox("電腦無法連接.") My.Computer.Audio.PlaySystemSound( _ System.Media.SystemSounds.Asterisk) End If ==========程式========= ==========程式========= 程式5 My.Computer.Audio.Play( My.Resources.Form1Greeting, _ AudioPlayMode.Background) ==========程式========= ==========程式========= 程式6 PropertyGrid1.SelectedObject = My.Settings Dim userAttr As New System.Configuration.UserScopedSettingAttribute Dim attrs As New System.ComponentModel.AttributeCollection (userAttr) PropertyGrid1.BrowsableAttributes = attrs ==========程式========= ==========程式========= 程式7 Dim msnrequest As New com.msn.search.soap.SearchRequest msnrequest.Query = "Microsoft" Dim msnsearch As New com.msn.search.soap.MSNSearchService() Dim searchResponse As com.msn.search.soap.SearchResponse = msnsearch.Search(msnrequest) MsgBox(searchResponse.Responses.ToString) ==========程式========= ==========程式========= 程式8 MsgBox(My.WebServices.MSNSearchService. Search(msnrequest)) ==========程式========= ==========程式========= 程式9 Public ReadOnly Property Info() As _ Microsoft.VisualBasic.ApplicationServices Get Return _ Microsoft.VisualBasic.ApplicationServices.AssemblyInfo End Get End Property ==========程式========= ==========程式========= 程式10 Imports System.Net.NetworkInformation public bool Ping(string hostNameOrAddress, int timeout) { if (!this.IsAvailable) { throw ExceptionUtils.GetInvalidOperationException( "Network_NetworkNotAvailable", new string[0]); } PingReply reply1 = new Ping(). Send(hostNameOrAddress, timeout, this.PingBuffer); if (reply1.Status == IPStatus.Success) { return true; } return false; } ==========程式========= ==========程式========= 程式11 public string get_MyDocuments() { return SpecialDirectories.MyDocuments; } ==========程式========= ==========程式========= 程式12 Namespace My _ Module MyModule Private m_extension As _ New ThreadSafeObjectProvider(Of ExtendedUser) Public ReadOnly Property MyExtension() As ExtendedUser Get Return m_extension.GetInstance() End Get End Property End Module End Namespace ==========程式========= ==========程式========= 程式13 #If _MyType = "WindowsForms" Then #Const _MYFORMS = True #Const _MYAPPLICATION = "WindowsForms" #Const _MYCOMPUTER = "Windows" #Const _MYUSER = "Windows" … #End If ==========程式=========