ฉันจะแปลงเส้นทางสัมพัทธ์เป็นเส้นทางสัมบูรณ์ในแอปพลิเคชัน Windows ได้อย่างไร
ฉันรู้ว่าเราสามารถใช้ server.MapPath () ใน ASP.NET แต่เราสามารถทำอะไรได้บ้างในแอปพลิเคชัน Windows?
ฉันหมายถึงถ้ามีฟังก์ชัน. NET ในตัวที่สามารถจัดการสิ่งนั้นได้ ...
ฉันจะแปลงเส้นทางสัมพัทธ์เป็นเส้นทางสัมบูรณ์ในแอปพลิเคชัน Windows ได้อย่างไร
ฉันรู้ว่าเราสามารถใช้ server.MapPath () ใน ASP.NET แต่เราสามารถทำอะไรได้บ้างในแอปพลิเคชัน Windows?
ฉันหมายถึงถ้ามีฟังก์ชัน. NET ในตัวที่สามารถจัดการสิ่งนั้นได้ ...
คำตอบ:
คุณได้ลอง:
string absolute = Path.GetFullPath(relative);
เหรอ? โปรดทราบว่าจะใช้ไดเร็กทอรีการทำงานปัจจุบันของกระบวนการไม่ใช่ไดเร็กทอรีที่มีไฟล์ปฏิบัติการ หากไม่ได้ผลโปรดชี้แจงคำถามของคุณ
หากคุณต้องการรับเส้นทางที่สัมพันธ์กับ. exe ของคุณให้ใช้
string absolute = Path.Combine(Application.ExecutablePath, relative);
Path.Combineไม่สามารถจัดการเส้นทางที่สัมพันธ์กับไดรฟ์ได้ มันไม่สนใจเส้นทางเริ่มต้นที่ดูเหมือน ฉันกำลังโพสต์วิธีแก้ปัญหาทั้งหมดของตัวเอง
..มันจะทำให้เกิดขยะ
..อยู่ในนั้น) ได้รับการยอมรับและแก้ไขอย่างสมบูรณ์แบบโดยระบบจัดการไฟล์ทั้งหมดใน. Net หากมันรบกวนคุณให้ดำเนินการabsolute = Path.GetFullPath(absolute)กับมัน
วิธีนี้ใช้ได้กับเส้นทางบนไดรฟ์ที่แตกต่างกันสำหรับเส้นทางที่สัมพันธ์กับไดรฟ์และสำหรับเส้นทางสัมพัทธ์จริง Heck แม้จะใช้งานได้หากbasePathไม่ได้เป็นแบบสัมบูรณ์จริง จะใช้ไดเร็กทอรีการทำงานปัจจุบันเป็นทางเลือกสุดท้ายเสมอ
public static String GetAbsolutePath(String path)
{
return GetAbsolutePath(null, path);
}
public static String GetAbsolutePath(String basePath, String path)
{
if (path == null)
return null;
if (basePath == null)
basePath = Path.GetFullPath("."); // quick way of getting current working directory
else
basePath = GetAbsolutePath(null, basePath); // to be REALLY sure ;)
String finalPath;
// specific for windows paths starting on \ - they need the drive added to them.
// I constructed this piece like this for possible Mono support.
if (!Path.IsPathRooted(path) || "\\".Equals(Path.GetPathRoot(path)))
{
if (path.StartsWith(Path.DirectorySeparatorChar.ToString()))
finalPath = Path.Combine(Path.GetPathRoot(basePath), path.TrimStart(Path.DirectorySeparatorChar));
else
finalPath = Path.Combine(basePath, path);
}
else
finalPath = path;
// resolves any internal "..\" to get the true full path.
return Path.GetFullPath(finalPath);
}
Path.Combine. แก้ไขได้ง่าย แต่ฉันหลีกเลี่ยงเนื่องจากฉันมักใช้เพื่อแก้ไขเส้นทางสัมพัทธ์บนไดเร็กทอรีการทำงานและการให้ null เนื่องจากอาร์กิวเมนต์แรกดูแปลก ๆ
เป็นหัวข้อที่เก่ากว่าเล็กน้อย แต่อาจมีประโยชน์สำหรับใครบางคน ฉันได้แก้ไขปัญหาที่คล้ายกันแล้ว แต่ในกรณีของฉันเส้นทางไม่ได้อยู่ที่จุดเริ่มต้นของข้อความ
นี่คือทางออกของฉัน:
public static class StringExtension
{
private const string parentSymbol = "..\\";
private const string absoluteSymbol = ".\\";
public static String AbsolutePath(this string relativePath)
{
string replacePath = AppDomain.CurrentDomain.BaseDirectory;
int parentStart = relativePath.IndexOf(parentSymbol);
int absoluteStart = relativePath.IndexOf(absoluteSymbol);
if (parentStart >= 0)
{
int parentLength = 0;
while (relativePath.Substring(parentStart + parentLength).Contains(parentSymbol))
{
replacePath = new DirectoryInfo(replacePath).Parent.FullName;
parentLength = parentLength + parentSymbol.Length;
};
relativePath = relativePath.Replace(relativePath.Substring(parentStart, parentLength), string.Format("{0}\\", replacePath));
}
else if (absoluteStart >= 0)
{
relativePath = relativePath.Replace(".\\", replacePath);
}
return relativePath;
}
}
ตัวอย่าง:
Data Source=.\Data\Data.sdf;Persist Security Info=False;
Data Source=..\..\bin\Debug\Data\Data.sdf;Persist Security Info=False;
Path.GetFullPathแก้ไข. \ และ .. \ โดยอัตโนมัติ นอกจากนี้คุณกำลังเพิ่มAbsolutePathฟังก์ชันส่วนขยายในคลาส String โดยทั่วไป ... อาจจะมากเกินไป