[英]How can I set a relative path in App.Config?
我正在使用WPF C#和Visual Studio 2017。
用來設置appSettings的文件路徑。 我想在App.Config文件中定義一個相對路徑。 如果我嘗試使用絕對路徑,它會起作用。
這項工作:
<appSettings file="C:\Users\jordan\AppData\Roaming\Test\appfile.config">
但是,如果我嘗試使用相對路徑,它將無法正常工作。
這不起作用:
<appSettings file="${AppData}\Roaming\Test\appfile.config">
<appSettings file="~\AppData\Roaming\Test\appfile.config">
<appSettings file="~/AppData/Roaming/Test/appfile.config">
etc...
您是否有想法在此處設置相對路徑? 還是為什么我不能做到這一點?
謝謝
當前漫游用戶的特定於應用程序的文件夾由枚舉Environment.SpecialFolder中的一個條目定義,它是ApplicationData條目
您可以使用Environment.GetFolderPath傳遞枚舉條目來檢索與此枚舉關聯的路徑。
string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
因此,在您的配置文件中,您可以僅存儲最后一部分Test \\ appfile.config並使用此代碼
string appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string configLocation = Configuration.AppSettings["file"].ToString();
string file = Path.Combine(appData, configLocation);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.