删除WPF中的硬编码文件夹路径

时间:2012-04-19 05:55:31

标签: c# wpf c#-4.0

在我的项目中,我指的是一个文件夹:

string path=Path.Combine(@"E:\Per\kamlendra.sharma\Windows\main\software\my.software\my.software.Server\Resources", string.Format("LocalizationDictionary.{0}.xaml", SelectedNewLanguage.culture));

但我不想硬编码这个地址:

@"E:\Per\kamlendra.sharma\Windows\main\software\my.software\my.software.Server\Resources"

有人可以建议更好的方法吗?

3 个答案:

答案 0 :(得分:4)

您可以将应用程序数据存储在app.config

答案 1 :(得分:1)

可以获得当前正在执行的程序集的UNC路径。然后,您可以使用此作为访问特定子文件夹的基础 - 这是假设您要查找的文件夹是程序集所在的子文件夹...

System.Reflection.Assembly.GetExecutingAssembly().Location //This actually returns the assembly file name, so you would need to use FileInfo to get the folder location.

更好的方法可能是System.Appdomain,它允许您访问实际WPF应用程序的位置而不是程序集。

System.AppDomain.CurrentDomain.BaseDirectory

答案 2 :(得分:0)

这是配置和使用应用程序数据文件的方法。