获取资源文件夹路径c#

Isa*_*ron 6 c# path

我在项目中的一些资源很好,使用字符串路径工作正常但如果我将项目移动到另一个目录或另一台计算机,它将停止工作.

我需要在字符串变量中获取项目的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)

这将返回您的资源数组.


Tej*_*jus 7

如果文件存储在项目文件夹中,则可以使用检索文件 System.AppDomain.CurrentDomain.BaseDirectory.此语句检索有关安装应用程序的位置的路径.点击这里获得详细解释.