Related Posts Plugin for WordPress, Blogger...

2014年9月1日 星期一

VB.Net 應用程式Snippet-編譯,資源和設定之程式碼片段

應用程式類的Visual Studio2013 Snippet,
包含了許多好用項目,
其中應用程式類的程式碼片段包含- 編譯,資源和設定
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



9.應用程式-編譯,資源和設定
9.1以名稱啟動執行中的應用程式: 使用應用程式的名稱啟動執行中的應用程式。
AppActivate("Untitled - Notepad")

9.2以處理序 ID 啟動執行中的應用程式: 使用應用程式的處理序 ID 啟動執行中的應用程式。
Dim processID As Integer
processID = Shell("NOTEPAD.EXE", AppWinStyle.NormalFocus)
AppActivate(processID)

9.3從主控台應用程式擷取輸出: 將命令主控台應用程式的輸出儲存到字串中。
Dim consoleApp As New Process
With consoleApp
    .StartInfo.UseShellExecute = False
    .StartInfo.RedirectStandardOutput = True
    .StartInfo.FileName = "ConsoleApplication.exe"
    .Start()
    .WaitForExit()
End With
Dim output As String = consoleApp.StandardOutput.ReadToEnd()

9.4變更主控台視窗中的前景和背景色彩: 變更主控台視窗的背景和文字色彩。
Console.BackgroundColor = ConsoleColor.DarkRed
Console.ForegroundColor = ConsoleColor.Gray
Console.Clear()

9.5使用 ClickOnce 檢查目前的版本: 使用 ClickOnce 檢查目前的版本。
If My.Application.IsNetworkDeployed Then
     currentAppVersion = My.Application.Deployment.CurrentVersion
     lastUpdateDate = My.Application.Deployment.TimeOfLastUpdateCheck
End If

9.6檢查 ClickOnce 應用程式的更新: 檢查是否有可供下載的應用程式更新。
If My.Application.IsNetworkDeployed()
     If My.Application.Deployment.CheckForUpdate() Then

     End If
End If

9.7清除主控台視窗: 清除主控台視窗。
Console.Clear()

9.8從隔離儲存區刪除檔案: 從隔離儲存區刪除檔案。
Dim isolatedStorage = IsolatedStorageFile.GetStore(IsolatedStorageScope.User Or IsolatedStorageScope.Assembly Or IsolatedStorageScope.Domain, Nothing, Nothing)
isolatedStorage.DeleteFile("FullPath\FileName.txt")

9.9判斷可用的隔離儲存區量: 判斷可用的隔離儲存區量。
Dim isolatedStorage = IsolatedStorageFile.GetStore(IsolatedStorageScope.User Or IsolatedStorageScope.Assembly Or IsolatedStorageScope.Domain, Nothing, Nothing)
Dim spaceAvailable = isolatedStorage.Quota - isolatedStorage.UsedSize

9.10判斷隔離儲存區的範圍: 判斷隔離儲存區的範圍。
Dim scope = isolatedStorage.Scope

9.11使用 ClickOnce 同步下載檔案: 使用 ClickOnce 同步下載檔案。
If My.Application.IsNetworkDeployed Then
      If Not(My.Application.Deployment.IsFileGroupDownloaded("Media")) Then
            My.Application.Deployment.DownloadFileGroup("Media")
      End If
End If

9.12尋找組件中資源的名稱: 傳回組件中所有資源的清單。
executingAssembly = Assembly.GetExecutingAssembly()
Dim resources() As String = executingAssembly.GetManifestResourceNames

9.13由組件載入游標檔: 由應用程式內嵌資源的游標檔,設定 Windows 表單的游標。
Dim newCursor As New Cursor(My.Resources.ResourceManager.GetStream("CursorResourceName"))
Me.Cursor = newCursor

9.14在執行階段載入組件: 在執行階段載入組件。
loadedAssembly = Assembly.LoadFile("C:\Folder\AssemblyName.dll")

9.15產生非同步方法呼叫: 使用 BackgroundWorker 元件產生非同步方法呼叫。
Private Sub startBackgroundTask()
        ' Execute the Background Task
        BackgroundWorker1.RunWorkerAsync()
End Sub
Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, _
ByVal e As System.ComponentModel.DoWorkEventArgs) _
Handles BackgroundWorker1.DoWork
    ' This method will execute in the background thread created by the BackgroundWorker componet

End Sub

Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As Object, _
 ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles _
BackgroundWorker1.RunWorkerCompleted
' This event fires when the DoWork event completes

End Sub

9.16由主控台視窗讀取和寫入: 撰寫提示,然後在使用者要求中讀取。這個提示會由主控台應用程式執行。
Console.WriteLine("Type in a sentence and hit Enter:")
inputFromConsole = Console.ReadLine()
Console.WriteLine(outputToConsole)

9.17讀取命令列的引數: 逐一查看命令列的引數。
For Each argument In My.Application.CommandLineArgs

Next


9.18由隔離儲存區讀取文字: 由隔離儲存區中的檔案讀取文字。
Dim isolatedStorage = IsolatedStorageFile.GetStore(IsolatedStorageScope.User _
Or IsolatedStorageScope.Assembly Or IsolatedStorageScope.Domain,_
 Nothing, Nothing)
Dim isolatedStream As New IsolatedStorageFileStream("myfile.txt", FileMode.Open, isolatedStore)

Using reader As New StreamReader(isolatedStream)
     InputText = reader.ReadToEnd
End Using

9.19擷取本身為應用程式資源的影像: [我的應用程式] 設計工具中的影像資源指派至 PictureBox1 Image 屬性。
PictureBox1.Image = My.Resources.ImageResource

9.20由組件的資源擷取文字檔: 由本身為組件內嵌資源的檔案中擷取文字。
Dim executingAssembly As Assembly = Assembly.GetExecutingAssembly
Dim appStream As Stream = _
executingAssembly.GetManifestResourceStream( _
"AssemblyName.Filename.txt")
Dim textStream As New StreamReader(appStream)
Dim text As String = textStream.ReadToEnd

9.21將文字儲存至隔離儲存區: 將字串寫入組件或使用者隔離儲存區中。
Dim isolatedStorage = IsolatedStorageFile.GetStore( _
IsolatedStorageScope.User Or IsolatedStorageScope.Assembly _
Or IsolatedStorageScope.Domain, Nothing, Nothing)
Dim isoStream As New IsolatedStorageFileStream("TestStore.txt", _
FileMode.Append, FileAccess.Write, isolatedStore)
 Using writer As New StreamWriter(isoStream)
     writer.WriteLine("The Data")
End Using

9.22傳送按鍵至應用程式: 將按鍵傳送至應用程式。
SendKeys.SendWait("{ENTER}")

9.23停止應用程式: 關閉「記事本」的所有執行中執行個體。
For Each proc In Process.GetProcessesByName("Untitled - Notepad")
     proc.CloseMainWindow()
Next

9.24使用 ClickOnce 以非同步方式更新應用程式: 以非同步方式更新 ClickOnce 應用程式。
If My.Application.IsNetworkDeployed Then
     My.Application.Deployment.UpdateAsync()
End If

9.25使用 ClickOnce 同步更新應用程式: 同步更新 ClickOnce 應用程式。
If My.Application.IsNetworkDeployed Then
    My.Application.Deployment.Update()
End If

9.26將訊息寫入應用程式記錄檔: 將訊息寫入應用程式記錄檔中。
My.Application.Log.WriteEntry("Action complete.")

9.27寫入文字檔: 將訊息寫入應用程式事件記錄檔。
My.Computer.FileSystem.WriteAllText("TheFile.txt", "TextContents", False)



相關貼文

沒有留言:

張貼留言

頁次