简体   繁体   中英

Path.GetFullPath returning wrong path in C#

According to the MSDN docs, to get the full file path of a file use

var fileName = Path.GetFullPath("KeywordsAndPhrases.txt");

Which I would assume delivers the full path of that file for the project it is in.

'K:\\HeadlineAutoTrader\\Autotrader.Infrastructure.HeadlineFeeds\\Data\\KeywordsAndPhrases.txt'

Which it doesn't, it returns;

'K:\\HeadlineAutoTrader\\AutotraderTests\\bin\\Debug\\Data\\KeywordsAndPhrases.txt'

which somewhat makes sense as the code I'm testing is being run in the test project.

So the question is how does one get the full filepath of a text file in a folder in another class library within the same solution?

This is a WPF based project, obviously it would be easier in a web app as Server.MapPath works great

From 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.

You can investigate the current working directory by using Environment.CurrentDirectory .

So the method is behaving as documented.

If you want the path relative to the main executable, take a look at What is the best way to get the executing exe's path in .NET?

When you call File.GetFullPath , the library looks at the current directory for the executing program. Unless otherwise specified, this is the same folder the executable is in.

You likely have "Copy to Output Directory" set to "Copy always" or "Copy if newer" in the properties of the KeywordsAndPhrases.txt file in Visual Studio, meaning it is copied to the same folder as the executable. Otherwise, the file may have been copied there by some other process (eg manually, post build step batch file). That is why it finds the file under K:\\HeadlineAutoTrader\\AutotraderTests\\bin\\Debug instead of 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?

The question does not make sense. Executables do not know about class libraries or the structure of files within a Visual Studio project. Executables know about either resources embedded in assemblies or files in folders. If you want to find the full path of a particular file, either set the current directory to that folder using Environment.CurrentDirectory or use a known relative path.

The path you are getting is techincally correct because you're referencing it from the bin\\debug folder in your code here

var fileName = Path.GetFullPath("KeywordsAndPhrases.txt");

if you were to get a file from somewhere like

var dir = Environment.SpecialFolder.ProgramFilesX86;
var path = Path.Combine(dir.ToString(), "file.txt");

then

var fileName = Path.GetFullPath(path);

then you would get the correct result which in this example is

c:\programfiles(86)\file.txt

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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