【发布时间】:2016-10-03 13:18:25
【问题描述】:
【问题讨论】:
-
我试过了,但我得到了 exe 路径,但我想要 wpf 应用程序的根路径
-
然后指定你的目的。要将文件作为资源存储在项目中还是输出目录中?
-
1.将路径放在配置文件中 2. 在代码中使用它 3. 利润
【问题讨论】:
获取表示路径的字符串:
string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
您可能想要执行以下操作:
DirectoryInfo directory = new DirectoryInfo(path);
根据根文件夹创建新路径:
var imagePath = path + @"\MyAppName\Images"
if (!Directory.Exists(imagePath))
{
DirectoryInfo di = Directory.CreateDirectory(imagePath);
}
编辑:作为替代方案,您可以使用特价文件夹。例如 LocalApplicationData,在此文件夹中,您可以为每个用户放置应用程序的机器范围数据。下面的变量 localData 将是
C:\Users\USER_NAME\AppData\Local
其中 USER_NAME 是用户个人资料。
string localData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
var imagePath = localData + @"\Images";
if (!Directory.Exists(imagePath))
{
DirectoryInfo di = Directory.CreateDirectory(imagePath);
}
【讨论】:
AppDomain.CurrentDomain.BaseDirectory 将为您提供应用程序文件夹的路径。
【讨论】: