我在项目中的一些资源很好,使用字符串路径工作正常但如果我将项目移动到另一个目录或另一台计算机,它将停止工作.
我需要在字符串变量中获取项目的resources文件夹的路径,
像这样的东西
C:\Users\User1\Documents\<projects folder>\<project name>\Resources\
Run Code Online (Sandbox Code Playgroud)
提前致谢.
Jay*_*obs 12
如果您知道相对于应用程序运行位置的路径,您可以执行类似的操作.首先,获取应用程序的运行路径:
string RunningPath = AppDomain.CurrentDomain.BaseDirectory;
Run Code Online (Sandbox Code Playgroud)
然后,使用以下内容导航到相对路径:
string FileName = string.Format("{0}Resources\\file.txt", Path.GetFullPath(Path.Combine(RunningPath, @"..\..\")));
Run Code Online (Sandbox Code Playgroud)
在这个例子中,我的"Resources"文件夹位于我正在运行的两个目录中.
我还应该提一下,如果您的资源包含在项目中,您应该能够使用它:
System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames();
Run Code Online (Sandbox Code Playgroud)
这将返回您的资源数组.