基本類的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
8.基本-集合,資料類型,檔案系統,數學
8.1集合和陣列
8.1.1在陣列中尋找元素: 在陣列中尋找指定元素的索引。
Dim index As Integer
index = Array.IndexOf(myArray,
"value")
|
8.1.2使用索引鍵在強類型字典中索引: 使用索引鍵在強類型字典中索引。
Dim capitol As String = stateCaps.Item("NM")
|
8.1.3依據索引鍵逐一查看排序的字典: 依據索引鍵排序清單逐一查看字典中的所有項目。
' Loop through the
items based on key
For Each rank As Integer In
sortedStudents.Keys
Dim student As String = sortedStudents.Item(rank)
Next
|
8.1.4初始化一維陣列: 初始化一維陣列。
Dim oneDimArray() As Integer = {1, 2, 3}
|
8.1.5定義排序常式中使用的類型比較子: 比較兩個物件並傳回值,指出其中一個物件為小於、等於或大於另一個物件。
Public Class IntegerComparer
Inherits Comparer(Of Integer)
Public Overrides Function Compare(ByVal param1 As Integer, _
ByVal param2 As Integer) As Integer
If param1 <
param2 Then
' result = <0
if param1 < param2
Return -1
ElseIf param1 >
param2 Then
' result = >0
if param1 > param2
Return 1
Else
' result = 0 if
param1 = param2
Return 0
End If
End Function
End Class
|
8.1.6建立含有一個項目類型的索引集合: 建立儲存以索引鍵索引的特殊項目類型的字典。
' Backing storage
-- a generic dictionary
Dim stateCaps As New Dictionary(Of String, String)
' Add items to the
dictionary
stateCaps.Add(
"WA" , "Olympia")
stateCaps.Add(
"NM" , "Santa Fe")
|
8.1.7建立含有單一類型項目的清單: 建立儲存特殊類型之項目的集合。
' Backing storage
-- a generic list
Dim names As New List(Of String)()
' Add an item to
the Collection
names.Add("John")
|
8.1.8建立排序的字典: 建立排序的字典,以儲存特定的項目類型。
' Backing storage
-- a generic dictionary
Dim sortedStudents As New SortedDictionary(Of Integer, String)
' Add items to the
dictionary
sortedStudents.Add(1,
"Mary Chase")
sortedStudents.Add(2,
"Barnaby Williams")
|
8.1.9將ArrayList轉換成強類型陣列: 使用 ToArray(Type) 將 ArrayList 轉換成強類型陣列
Dim newArray() As String = CType(existingArrayList.ToArray(
_
GetType(String)), String())
|
8.1.10排序陣列: 宣告字串物件的陣列、填入該陣列,然後依字母順序排序該陣列。
Dim animals() As String = {"lion", "turtle", "ostrich"}
Array.Sort(animals)
|
8.1.11逐一查看字典: 逐一查看字典中的所有項目。
' Iterate through
a dictionary
For Each capitol As String In stateCaps.Values
Next
|
8.1.12逐一查看集合: 逐一查看集合中的特定項目類型。
' Iterate through
a collection
For Each name As String In names
Next
|
8.2資料類型
8.2.1在平假名和片假名字元之間轉換 (僅限日文): 在字串中轉換平假名和片假名字元。
' Convert Katakana
to Hiragana (Japanese Only).
Dim hString As String = StrConv("Katakana
String",
_
VbStrConv.Hiragana)
' Convert Hiragana
to Katakana (Japanese Only).
Dim kString As String = StrConv("Hiragana
String",
_
VbStrConv.Katakana)
|
8.2.2在寬字元和窄字元之間轉換: 在字串中轉換寬字元和窄字元。
' Convert Wide to
Narrow.
Dim nString As String = StrConv("Wide
String",
VbStrConv.Narrow)
' Convert Narrow
to Wide.
Dim wString As String = StrConv("Narrow
String",
VbStrConv.Wide)
|
8.2.3在簡體中文和繁體中文字元之間轉換: 在字串中轉換簡體中文和繁體中文字元。
' Convert
Simplified Chinese to Traditional Chinese.
Dim tString As String = StrConv("Simplified
Chinese String", _
VbStrConv.TraditionalChinese)
' Convert
Traditional Chinese to Simplified Chinese.
Dim sString As String = StrConv("Traditional
Chinese String", _
VbStrConv.SimplifiedChinese)
|
8.2.4判斷兩個日期之間的時間長度: 計算兩個日期之間相差的日數,同時為此差異建構 TimeSpan 值。
Dim oldDate As Date = #1/1/2002#
Dim newDate As Date = Now
' You can also
determine the difference in times in other units.
Dim difference As TimeSpan =
newTime - oldTime
Dim differenceInDays As Double =
difference.TotalDays
|
8.2.5判斷兩個時間之間的時間長度: 計算同一天中兩個時間相差的秒數,同時為此差異建構 TimeSpan 值。
Dim oldTime As Date = Today
Dim newTime As Date = Now
' You can also
determine the difference in times in other units.
Dim difference As TimeSpan =
newTime - oldTime
Dim
differenceInSeconds As Double = difference.TotalSeconds
|
8.2.6析電子郵件地址: 由電子郵件地址傳回使用者名稱和提供者名稱。
Dim emailParts() As String
Dim user As String
Dim provider As String
emailParts =
"SomeName@SomeCompany.com".Split("@".ToCharArray, 2)
If emailParts.Length
= 2 Then
user = emailParts(0)
provider = emailParts(1)
Else
' Email address is not valid. The expected format
is user@provider.*
user = String.Empty
provider = String.Empty
End If
|
8.2.7將
Byte() 轉換成影像: 將 Byte() 轉換成影像。位元組陣列通常是影像資訊儲存在資料庫中的方式
Public Function
GetImageFromByteArray(ByVal bytes As Byte()) As Bitmap
Return CType(Bitmap.FromStream(New
IO.MemoryStream(bytes)), Bitmap)
End Function
|
8.2.8將字串轉換為位元組陣列: 將字串轉換為位元組陣列。
Dim bytes As Byte() =
Encoding.Unicode.GetBytes("StringToConvert")
|
8.2.9將位元組陣列轉換為字串: 將位元組陣列轉換為字串
Private Function
ConvertByteArrayToString(ByVal byteArray As Byte()) As String
Dim enc As Encoding =
Encoding.UTF8
Dim text As String =
enc.GetString(byteArray)
Return text
End Function
|
8.2.10將影像轉換成 Byte():將影像轉換成 Byte()。位元組陣列通常是影像資訊儲存在資料庫中的方式
Public Function
GetByteArrayFromImage(ByVal img As Bitmap) As Byte()
Dim ms As New
System.IO.MemoryStream
img.Save(ms, Imaging.ImageFormat.Bmp)
Dim outBytes(CInt(ms.Length - 1)) As Byte
ms.Seek(0,
System.IO.SeekOrigin.Begin)
ms.Read(outBytes, 0, CInt(ms.Length))
Return outBytes
End Function
|
8.2.11將數字轉換為八進位字串: 傳回整數的八進位字串表示。
Dim octal As String = Oct(48)
|
8.2.12將數字轉換為十六進位字串: 傳回整數的十六進位表示。
Dim hexString As String = 48.ToString("X")
|
8.2.13產生多行字串常值: 建構長度為三行的字串常值。
Dim longString As String
longString = _
"This is the first line of my string." &
Environment.NewLine & _
"This is the second line of my string." &
Environment.NewLine & _
"This is the third line of my string."
|
8.2.14移除字串的部分: 從一個字串中移除出現另一個字串的所有位置。
Dim withParts As String = "Books and
Chapters and Pages"
Dim withoutParts As String =
withParts.Replace("and ", "")
|
8.3數學
8.3.1使用 Random 類別取得亂數: 產生介於上限和下限之間的隨機整數。
Dim generator As New Random
Dim randomValue As Integer
randomValue =
generator.Next(10, 100)
|
8.3.2計算年數合計折舊: 傳回指定資產經過一段指定期間後,年數合計折舊數的雙精度浮點數。
Dim depreciation As Double
depreciation =
SYD(1000, 200, 3, 2005)
|
8.3.3計算指定角度的正切函數: 將角度的單位從角度轉換成弧度,然後計算角度的正切函數
Dim radians As Double = 120 * Math.PI /
180
Dim tan As Double =
Math.Tan(radians)
|
8.3.4計算指定角度的正弦函數: 將角度的單位從角度轉換成弧度,然後計算角度的正弦函數
Dim radians As Double = 120 * Math.PI /
180
Dim sin As Double =
Math.Sin(radians)
|
8.3.5計算指定角度的餘弦函數: 將角度的單位從角度轉換成弧度,然後計算角度的餘弦函數
Dim radians As Double = 120 * Math.PI /
180
Dim cos As Double = Math.Cos(radians)
|
8.3.6計算貸款的每月付款金額: 計算貸款的每月付款金額。
Dim futureValue As Double = 0
Dim payment As Double
payment = Pmt(.05
/ 12, 36, -1000, futureValue, DueDate.EndOfPeriod)
|
8.4檔案系統-處理磁碟,資料夾和檔案
8.4.1比較兩個檔案: 比較兩個檔案。
Private Function CompareFiles(ByVal file1 As String, ByVal file2 As String) As Boolean
' Set to true if the files are equal; false
otherwise
Dim filesAreEqual As Boolean = False
With My.Computer.FileSystem
' Ensure that the
files are the same length before comparing them line by line.
If
.GetFileInfo(file1).Length = .GetFileInfo(file2).Length Then
Using file1Reader As New FileStream(file1,
FileMode.Open), _
file2Reader As New FileStream(file2,
FileMode.Open)
Dim byte1 As Integer =
file1Reader.ReadByte()
Dim byte2 As Integer =
file2Reader.ReadByte()
' If byte1 or
byte2 is a negative value,
‘we have reached
the end of the file.
While byte1 >= 0 AndAlso byte2 >= 0
If (byte1 <>
byte2) Then
filesAreEqual = False
Exit While
Else
filesAreEqual = True
End If
' Read the next
byte.
byte1 =
file1Reader.ReadByte()
byte2 =
file2Reader.ReadByte()
End While
End Using
End If
End With
Return filesAreEqual
End Function
|
8.4.2由檔案讀取文字: 將文字檔的內容讀取至字串中。
Dim fileContents As String
fileContents =
My.Computer.FileSystem.ReadAllText("C:\Test.txt")
|
8.4.3列舉電腦上的磁碟: 列舉電腦上的磁碟。
For Each drive In
My.Computer.FileSystem.Drives
Next
|
8.4.4在目錄中遞迴搜尋檔案: 在目錄中遞迴搜尋檔案。
Dim files As ReadOnlyCollection(Of String)
files =
My.Computer.FileSystem.GetFiles("C:\", FileIO.SearchOption.SearchAllSubDirectories,
"*.txt")
|
8.4.5在檔案中搜尋運算式: 在目錄中的檔案內搜尋運算式。
Dim files As
ReadOnlyCollection(Of String)
files =
My.Computer.FileSystem.FindInFiles("C:\", "Text To Search For", True,
FileIO.SearchOption.SearchAllSubDirectories, "*.txt")
|
8.4.6判斷資料夾是否存在: 設定布林值變數,表示指定的資料夾是否存在。
Dim folderExists As Boolean
folderExists =
My.Computer.FileSystem.DirectoryExists("C:\TestDirectory")
|
8.4.10判斷檔案大小: 取得檔案大小,並指派給變數
Dim fileSize As Long
fileSize =
My.Computer.FileSystem.GetFileInfo("filename.txt").Length
|
8.4.7判斷檔案是否存在: 設定布林值變數,表示指定的檔案是否存在。
Dim fileExists As Boolean
fileExists =
My.Computer.FileSystem.FileExists("C:\Test.txt")
|
8.4.8刪除檔案: 使用者確認刪除之後刪除檔案。
My.Computer.FileSystem.DeleteFile("C:\Test.txt",
_
FileIO.UIOption.AllDialogs, _
FileIO.RecycleOption.SendToRecycleBin)
|
8.4.9取得檔案的相關資訊: 取得包含檔案相關資訊的物件。
Dim fileData As FileInfo = My.Computer.FileSystem.GetFileInfo("C:\Test.txt")
|
8.4.10建立資料夾: 在此電腦上建立新資料夾。
My.Computer.FileSystem.CreateDirectory("C:\NewDirectory")
|
8.4.11建立暫存檔案名稱: 建立暫存檔案的名稱。
Dim fileName As String
fileName =
My.Computer.FileSystem.GetTempFileName()
|
8.4.12建立檔案: 建立新檔案。
My.Computer.FileSystem.WriteAllText("C:\Test.txt",
_
String.Empty, False)
|
8.4.12重新命名目錄: 重新命名目錄。
My.Computer.FileSystem.RenameDirectory("C:\OldDirectory",
_
"NewDirectory")
|
8.4.13重新命名檔案: 重新命名檔案。
My.Computer.FileSystem.RenameFile("C:\OldFilename.txt",
_
"NewFilename.txt")
|
8.4.14將文字寫入檔案: 將一行文字寫入文字檔中,如果檔案存在則取代任何文字。
My.Computer.FileSystem.WriteAllText("C:\Test.txt",
"Text", True)
|
8.4.15移動檔案: 將檔案移動到新的目錄。
My.Computer.FileSystem.MoveFile("C:\OldDirectory\File.txt",
_
"c:\NewDirectory\File.txt")
|
8.4.16尋找可用的磁碟空間量: 取得可用的磁碟空間量。
Dim freeSpace As Long
freeSpace =
My.Computer.FileSystem.GetDriveInfo("C:\").TotalFreeSpace
|
8.4.17寫入二進位檔案: 將位元組寫入二進位檔案。
Dim fileContents() As Byte = {244, 123, 56,
34}
My.Computer.FileSystem.WriteAllBytes("C:\Output.bin",
fileContents, True)
|
8.4.18複製目錄: 複製目錄及其所有檔案和子目錄。
My.Computer.FileSystem.CopyDirectory("C:\SourceDirectory",
"D:\DestinationDirectory")
|
8.4.19複製檔案: 將 Source.txt 檔複製到另一個檔案 Copy.txt 中。
My.Computer.FileSystem.CopyFile("C:\Source.txt",
"C:\NewFolder\Dest.txt")
|
8.4.20擷取本機電腦上的磁碟: 擷取本機電腦上的磁碟名稱。
Dim driveNames As New List(Of String)
For Each drive As DriveInfo In
My.Computer.FileSystem.Drives
driveNames.Add(drive.Name)
Next
|
8.4.21讀取 [我的文件] 資料夾中的檔案: 將 [我的文件] 資料夾中文字檔的內容,讀取至單一字串中。
Dim fullFilePath As String
Dim fileContents As String
With
My.Computer.FileSystem
fullFilePath =
.CombinePath(.SpecialDirectories.MyDocuments, "test.txt")
fileContents = .ReadAllText(fullFilePath)
End With
|
8.4.22讀取二進位檔案: 由二進位檔案讀取資料。
Dim fileContents As Byte()
filecontents =
My.Computer.FileSystem.ReadAllBytes("C:\Test.txt")
|
8.4.23讀取以符號分隔的文字檔: 由以逗號分隔之資料的文字檔中,傳回每一欄和列的值。也可能會使用其他分隔符號。
Dim filename As String = "C:\Test.txt"
Dim fields As String()
Dim delimiter As String = ","
Using parser As New TextFieldParser(filename)
parser.SetDelimiters(delimiter)
While Not parser.EndOfData
' Read in the fields for the current line
fields = parser.ReadFields()
' Add code here to use data in fields variable.
End While
End Using
|
相關貼文
沒有留言:
張貼留言