Path.GetFullPath 在 C# 中返回错误的路径
Path.GetFullPath returning wrong path in C#
根据 MSDN 文档,要获取文件的完整文件路径,请使用
var fileName = Path.GetFullPath("KeywordsAndPhrases.txt");
我假设它提供了该文件所在项目的完整路径。
'K:\HeadlineAutoTrader\Autotrader.Infrastructure.HeadlineFeeds\Data\KeywordsAndPhrases.txt'
它不是,它 returns;
'K:\HeadlineAutoTrader\AutotraderTests\bin\Debug\Data\KeywordsAndPhrases.txt'
这有点道理,因为我正在测试的代码在测试项目中 运行。
所以问题是如何在同一解决方案中的另一个 class 库中的文件夹中获取文本文件的完整文件路径?
这是一个基于 WPF 的项目,显然在 Web 应用程序中会更容易,因为 Server.MapPath 效果很好
来自https://msdn.microsoft.com/en-us/library/system.io.path.getfullpath%28v=vs.110%29.aspx
This method uses current directory and current volume information to fully qualify path. If you specify a file name only in path, GetFullPath returns the fully qualified path of the current directory.
您可以使用 Environment.CurrentDirectory.
调查当前 working 目录
所以该方法的行为与记录的一样。
如果你想要相对于主要可执行文件的路径,请查看 What is the best way to get the executing exe's path in .NET?
您获得的路径在技术上是正确的,因为您在此处的代码 bin\debug 文件夹中引用了它
var fileName = Path.GetFullPath("KeywordsAndPhrases.txt");
如果您要从
这样的地方获取文件
var dir = Environment.SpecialFolder.ProgramFilesX86;
var path = Path.Combine(dir.ToString(), "file.txt");
然后
var fileName = Path.GetFullPath(path);
那么你会得到正确的结果,在这个例子中是
c:\programfiles(86)\file.txt
当您调用 File.GetFullPath
时,库会查看正在执行的程序的当前目录。除非另有说明,否则这是可执行文件所在的同一文件夹。
您可能在 Visual Studio 中 KeywordsAndPhrases.txt
文件的属性中将 "Copy to Output Directory" 设置为 "Copy always" 或 "Copy if newer",这意味着它被复制到与可执行文件相同的文件夹。否则,该文件可能已被其他一些进程复制到那里(例如手动,post 构建步骤批处理文件)。这就是为什么它在 K:\HeadlineAutoTrader\AutotraderTests\bin\Debug
而不是 K:\HeadlineAutoTrader\Autotrader.Infrastructure.HeadlineFeeds
.
下找到文件的原因
How does one get the full filepath of a text file in a folder in another class library within the same solution?
这个问题没有意义。可执行文件不知道 class 库或 Visual Studio 项目中的文件结构。可执行文件知道程序集中嵌入的资源或文件夹中的文件。如果要查找特定文件的完整路径,请使用 Environment.CurrentDirectory 将当前目录设置为该文件夹或使用已知的相对路径。
根据 MSDN 文档,要获取文件的完整文件路径,请使用
var fileName = Path.GetFullPath("KeywordsAndPhrases.txt");
我假设它提供了该文件所在项目的完整路径。
'K:\HeadlineAutoTrader\Autotrader.Infrastructure.HeadlineFeeds\Data\KeywordsAndPhrases.txt'
它不是,它 returns;
'K:\HeadlineAutoTrader\AutotraderTests\bin\Debug\Data\KeywordsAndPhrases.txt'
这有点道理,因为我正在测试的代码在测试项目中 运行。
所以问题是如何在同一解决方案中的另一个 class 库中的文件夹中获取文本文件的完整文件路径?
这是一个基于 WPF 的项目,显然在 Web 应用程序中会更容易,因为 Server.MapPath 效果很好
来自https://msdn.microsoft.com/en-us/library/system.io.path.getfullpath%28v=vs.110%29.aspx
This method uses current directory and current volume information to fully qualify path. If you specify a file name only in path, GetFullPath returns the fully qualified path of the current directory.
您可以使用 Environment.CurrentDirectory.
调查当前 working 目录所以该方法的行为与记录的一样。
如果你想要相对于主要可执行文件的路径,请查看 What is the best way to get the executing exe's path in .NET?
您获得的路径在技术上是正确的,因为您在此处的代码 bin\debug 文件夹中引用了它
var fileName = Path.GetFullPath("KeywordsAndPhrases.txt");
如果您要从
这样的地方获取文件var dir = Environment.SpecialFolder.ProgramFilesX86;
var path = Path.Combine(dir.ToString(), "file.txt");
然后
var fileName = Path.GetFullPath(path);
那么你会得到正确的结果,在这个例子中是
c:\programfiles(86)\file.txt
当您调用 File.GetFullPath
时,库会查看正在执行的程序的当前目录。除非另有说明,否则这是可执行文件所在的同一文件夹。
您可能在 Visual Studio 中 KeywordsAndPhrases.txt
文件的属性中将 "Copy to Output Directory" 设置为 "Copy always" 或 "Copy if newer",这意味着它被复制到与可执行文件相同的文件夹。否则,该文件可能已被其他一些进程复制到那里(例如手动,post 构建步骤批处理文件)。这就是为什么它在 K:\HeadlineAutoTrader\AutotraderTests\bin\Debug
而不是 K:\HeadlineAutoTrader\Autotrader.Infrastructure.HeadlineFeeds
.
How does one get the full filepath of a text file in a folder in another class library within the same solution?
这个问题没有意义。可执行文件不知道 class 库或 Visual Studio 项目中的文件结构。可执行文件知道程序集中嵌入的资源或文件夹中的文件。如果要查找特定文件的完整路径,请使用 Environment.CurrentDirectory 将当前目录设置为该文件夹或使用已知的相对路径。