顯示具有 軟體撰寫-VB 標籤的文章。 顯示所有文章
顯示具有 軟體撰寫-VB 標籤的文章。 顯示所有文章

2017年3月17日 星期五

不要讓迴圈吃完資源

            Application.DoEvents()
            System.Threading.Thread.Sleep(1)

數值顯示指定字串顯示位數,不足補0

Dim k as string="555"
Dim Q as string="ABC"

Debug.Print(K.PadLeft(6, "0"))   '顯示為000555

Debug.Print(K.PadLeft(6, "$"))   '顯示為$$$555   (注意,後面的字串只能一個位元)
Debug.Print(K.PadLeft(6, "$NT"))   '顯示為$$$555 (如果後面字串不是一個位元,則會以第一個位元為主)

Debug.Print(K.PadLeft(6, "Z"))   '顯示為ZZZABC

2017年1月11日 星期三

判斷路徑資料夾是否存在及建立資料夾

VB.NET
判別路徑資料夾是否存在:
 If Not IO.Directory.Exists("Path+資料夾名稱") Then

            '如不存在,建立資料夾
            IO.Directory.CreateDirectory("Path+資料夾名稱")
 End If


判別路徑檔案是否存在:
 System.IO.File.Exists("Path+FileName")

2015年7月15日 星期三

從文字檔讀取(Visual Basic)

程式碼:從文字檔讀取 (Visual Basic)

Visual Studio .NET 2003

這個範例使用 StreamReader 類別的 ReadToEnd 方法,將文字檔的內容讀入字串。

範例

Dim file As New System.IO.StreamReader("c:\test.txt")
Dim words As String = file.ReadToEnd()
file.Close()



資料來源  
https://msdn.microsoft.com/zh-tw/library/cc464082(v=vs.71).aspx

2015年6月10日 星期三

How to: Upload a File in Visual Basic

How to: Upload a File in Visual Basic

Visual Studio 2013
The UploadFile method can be used to upload a file and store it to a remote location. If the ShowUI parameter is set to True, a dialog box is displayed that shows the progress of the download and allows users to cancel the operation.

To upload a file

  • Use the UploadFile method to upload a file, specifying the source file's location and the target directory location as a string or URI (Uniform Resource Identifier).This example uploads the file Order.txt tohttp://www.cohowinery.com/uploads.aspx.
    My.Computer.Network.UploadFile(
      "C:\My Documents\Order.txt",
      "http://www.cohowinery.com/upload.aspx")
    

To upload a file and show the progress of the operation

  • Use the UploadFile method to upload a file, specifying the source file's location and the target directory location as a string or URI. This example uploads the file Order.txt tohttp://www.cohowinery.com/uploads.aspx without supplying a user name or password, shows the progress of the upload, and has a time-out interval of 500 milliseconds.
    My.Computer.Network.UploadFile(
      "C:\My Documents\Order.txt",
      "http://www.cohowinery.com/upload.aspx", "", "", True, 500)
    

To upload a file, supplying a user name and password

  • Use the UploadFile method to upload a file, specifying the source file's location and the target directory location as a string or URI, and specifying the user name and the password. This example uploads the fileOrder.txt to http://www.cohowinery.com/uploads.aspx, supplying the user name anonymous and a blank password.
    My.Computer.Network.UploadFile(
      "C:\My Documents\Order.txt",
      "http://www.cohowinery.com/upload.aspx", "anonymous", "")
    

The following conditions may throw an exception:

2014年9月25日 星期四

VB 寫檔案

1Dim sw As System.IO.StreamWriter = System.IO.File.AppendText("檔案路徑")
2sw.WriteLine("要寫出的內容")
3sw.Flush()
4sw.Close()
5sw.Dispose()
另外的寫法


寫一個純文字檔案步驟如下:
第一步:引用FileStream類別,下列語法已經不用判定檔案是否存不存在,因為已經使用了FileMode.OpenOrCreate參數。
//引用類別
FileStream myFile = File.Open(@"C:\myTest.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
 
第二步:引用StreamWriter類別
//引用StringWriter類別
StreamWriter myWriter = new StreamWriter(myFile);
 
第三步:寫檔
//寫字串至檔案
myWriter.Write("檔案建立於:");
myWriter.WriteLine(DateTime.Now);

第四步:釋放資源
//釋放資源
myWriter.Dispose();
myFile.Dispose();



 
讀一個純文字檔案步驟如下:
第一步:引用FileStream類別
//引用類別
FileStream myFile = File.Open(@"C:\myTest.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite);
第二步:引用StreamReader類別
//引用StringReader類別
StreamReader myReader = new StreamReader(myFile);
第三步:讀檔
//讀檔案
Console.Write(myReader.ReadToEnd());
第四步:釋放資源

//釋放資源
myWriter.Dispose();
myFile.Dispose();

 
寫一個純文字檔案步驟如下:只要將open的參數改一下就能續寫了
FileStream myFile = File.Open(@"C:\myTest.txt", FileMode.Append, FileAccess.Write, FileShare.ReadWrite)
 
後記:StreamReader/StreamWriter與StringReader/StringReader,兩組類別擇一使用即可,差別在於StringReader/StringReader在讀寫大量字串時效能較佳,也能將資料暫時寫入記憶體,整體來講StringReader/StringReader的功能似乎略勝一籌。

VB FOR 迴圈

Program that uses For: VB.NET

Module Module1
    Sub Main()
 ' This loop goes from 0 to 5.
 For value As Integer = 0 To 5
     ' Exit condition if the value is three.
     If (value = 3) Then
  Exit For
     End If
     Console.WriteLine(value)
 Next
    End Sub
End Module

Output

0
1
2
 
 
Program that uses For Step: VB.NET

Module Module1
    Sub Main()
 ' This loop uses Step to go down 2 each iteration.
 For value As Integer = 10 To 0 Step -2
     Console.WriteLine(value)
 Next
    End Sub
End Module

Output

10
8
6
4
2
0 
 
出處

常用VB字串處理函數

常用VB字串處理函數
使用內建函數:
Len:計算字串的長度。
範例:strLen=Len("Taiwan")
結果:6

Mid:依開始位置擷取固定長度字串。
範例:strLen=Mid("Taiwan",1,2)
結果:Ta

Left:將字串由左側擷取固定長度。
範例:strLen=Left("Taiwan",2)
結果:Ta
    

Right:將字串由右側擷取固定長度。
範例:str=Right(" Taiwan ",2)
結果:an

Lcase:將字串中的大寫字母變成小寫。
範例:str=Lcase("Taiwan")
結果:taiwan


Ucase:將字串中的小寫字母變成大寫。
範例:str=Ucase("Taiwan")
結果:TAIWAN

Trim:刪頭尾空白
範例:str=Trim("   Taiwan    ")
結果:Taiwan

Ltrim:刪字串左側的空白
範例:str=Ltrim("  Taiwan    ")
結果:Taiwan     

Rtrim:刪字串右側的空白
範例:str=Rtrim("  Taiwan    ")
結果:  Taiwan
刪字串 兩側     的空白

Replace:將字串中的某字串,以另一字串代替。
範例:str=Replace("Taiwan","Tai"," Nan ")
結果:Nanwan

Instr:傳回子字串在字串中的第一次出現位置。
範例:strLen=Instr("ABCabc","a")
結果:4

Space:產生指定個數空白。
範例:Space(10)
結果:4個空白

String:產生指定個數的字元。
範例:str=String(5,"$")
結果:$$$$$

範例:str=String(4,66)
結果:BBBB
    
Strreverse:將字串前後反轉。
範例:str=Strreverse(" Taiwan ")
結果:nawiaT

Split:字串分割
範例:str=Split("Taiwan","T")
結果:
str(0)="T"
str(1)="aiwan"