thongkorn โพสต์ 2017-10-25 09:47:40

[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"

MMEE007 โพสต์ 2020-2-20 01:26:58

ขอบพระคุณคับ อาจารย์

khantharod โพสต์ 2020-8-6 09:19:17

ขอบพระคุณคับ
หน้า: [1]
ดูในรูปแบบกติ: [VB.NET] การกำหนดตำแหน่งพาธให้กับโปรเจค