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



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

string path=Path.Combine(@"E:Perkamlendra.sharmaWindowsmainsoftwaremy.softwaremy.software.ServerResources", string.Format("LocalizationDictionary.{0}.xaml", SelectedNewLanguage.culture));

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

@"E:Perkamlendra.sharmaWindowsmainsoftwaremy.softwaremy.software.ServerResources"

任何人都可以提出更好的方法吗?

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

可以获取当前正在执行的程序集的 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

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

  • WPF 应用程序资源、内容和数据文件

最新更新