简体   繁体   English

导致 C# 中错误文件的相对路径

[英]Relative path leading to the wrong file in C#

I have a text file called GameSettings.txt which has the absolute path of:我有一个名为GameSettings.txt的文本文件,其绝对路径为:

C:\Users\User\Source\Repos\CLIBattleships\CLIBattleships\General\GameSettings.txt C:\Users\User\Source\Repos\CLIBattleships\CLIBattleships\General\GameSettings.txt

When I try to get its relative path to prevent issues when the project is running from another computer by doing:当我尝试通过执行以下操作获取其相对路径以防止项目从另一台计算机运行时出现问题时:

private static StreamReader sr = new StreamReader(@"General\GameSettings.txt");

It returns this path instead:它改为返回此路径:

C:\Users\User\Source\Repos\CLIBattleships\CLIBattleships\bin\Debug\netcoreapp3.1\General\GameSettings.txt C:\Users\User\Source\Repos\CLIBattleships\CLIBattleships\bin\Debug\netcoreapp3.1\General\GameSettings.txt

I looked up a few similar questions asked here, and the answers suggested that the directory executable is in is different from the current directory.我查找了一些类似的问题here,答案表明可执行文件所在的目录与当前目录不同。

The few upvoted answers were:少数赞成的答案是:

string filePath = System.IO.Path.GetFullPath("TestFile.txt"); 
StreamReader sr = new StreamReader(filePath);

and

string dir = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);

string file = dir + @"\TestDir\TestFile.txt";
// then use file as a parameter for the StreamReader

Both of them lead to the same, wrong (at least contextually wrong) relative path.它们都导致相同的错误(至少在上下文上是错误的)相对路径。 Please help.请帮忙。

Building the project compiles your code and copies the files into the bin folder.构建项目会编译您的代码并将文件复制到 bin 文件夹中。 Building and running in "Debug" preset(default) will output your files into bin/Debug/netcoreapp3.1/ folder.在“调试”预设(默认)中构建和运行会将 output 您的文件放入bin/Debug/netcoreapp3.1/文件夹中。 Here is where your .exe lies and where its relative path is situated.这是您的.exe所在的位置以及它的相对路径所在的位置。

Now, for your specific problem, a dirty way to fix this is to use .. inside relative path.现在,对于您的特定问题,解决此问题的一种肮脏方法是在相对路径中使用.. This will get you to the root of current directory.这将使您进入当前目录的根目录。 Say you state " ../../file.txt " as a path.假设您将 state “ ../../file.txt ” 作为路径。 This will look for the file.txt inside the bin folder.这将在 bin 文件夹中查找file.txt BUT - that is not how you should solve this issue.但是- 这不是你应该如何解决这个问题。 I just wanted to mention this as it's a useful detail to know and there are situations where you can use this to your advantage.我只是想提一下这一点,因为这是一个有用的细节,并且在某些情况下您可以利用它来发挥自己的优势。

I suggest adding your file either to project resources or as a plain file.我建议将您的文件添加到项目资源或作为纯文件。 Then, as somebody already mentioned, you can set that file as content and make the visual studio copy its contents to the output folder.然后,正如有人已经提到的那样,您可以将该文件设置为内容,并使 Visual Studio 将其内容复制到 output 文件夹。 That way, the file will be copied to the same directory where the executable lies, and your path to it will literally be just the name of the file.这样,文件将被复制到可执行文件所在的同一目录中,而您的路径实际上就是文件的名称。

Using the project resources, however, will not expose the files to the file system.但是,使用项目资源不会将文件暴露给文件系统。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM