[VB.NET] การกำหนดตำแหน่งพาธให้กับโปรเจค
ว่ากันง่ายๆ แอดมินเขียนโค้ดชุดนี้ขึ้นมาเพื่อทดแทน App.Path ใน VB6 ครับพี่น้องต้องขึ้นอยู่กับว่าเราตั้งค่าการ Build output path เอาไว้ที่ไหน
โดย Windows 64 บิท จะอยู่ที่
"\bin\debug"
"\bin\release"
หรือ Windows 32 บิท
"\bin\x86\debug"
"\bin\x86\release"
สมมุติอ่านค่า Application.StartupPath ได้เป็น
C:\Project VB.Net\ConnectAccessNET\bin\debug
เราจะถอยหลังโดยการตัด \bin\debug ทิ้งออกไป โดยการเปรียบเทียบค่าเพื่อให้เป็น
C:\Project VB.Net\ConnectAccessNET
' / --------------------------------------------------------------------------------
' / Get my project path
' / AppPath = C:\My Project\bin\debug
' / Replace "\bin\debug" with "\"
' / Return : C:\My Project\
Function MyPath(AppPath As String) As String
'/ MessageBox.Show(AppPath);
AppPath = AppPath.ToLower()
'/ Return Value
MyPath = AppPath.Replace("\bin\debug", "\").Replace("\bin\release", "\").Replace("\bin\x86\debug", "\")
'// If not found folder then put the \ (BackSlash ASCII Code = 92) at the end.
If Right(MyPath, 1) <> Chr(92) Then MyPath = MyPath & Chr(92)
End Functionเป็นฟังค์ชั่นที่รับค่า StartupPath และรีเทิร์นคืนค่ากลับแบบ String ตามที่เรากำหนด ...
(ข้อผิดพลาดในการแสดงผล เพราะมันไม่โชว์เครื่องหมาย \) แก้ไขบรรทัดก่อน End Function ...
If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
ยกตัวอย่างการนำไปใช้งาน
Dim strPathData As String = MyPath(Application.StartupPath) & "Data"
Dim strPathImages As String = MyPath(Application.StartupPath) & "Images"strPathData = "C:\Project VB.Net\ConnectAccessNET\data"
ขอบพระคุณคับ อาจารย์ ขอบพระคุณคับ
หน้า:
[1]