3

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

dinotom
  • 4,990
  • 16
  • 71
  • 139

3 Answers3

4

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 (e.g. 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.

akton
  • 14,148
  • 3
  • 43
  • 47
  • Ty, since I am trying to access this in the Unit Tests, it is just going to be easier to have a copy of these text files there, as they will only be used in their own executing assembly in production. – dinotom Nov 02 '15 at 23:22
  • and the property is set to "Do not copy" – dinotom Nov 02 '15 at 23:24
  • @dinotom Updated answer to include other possible reasons why the file is there. – akton Nov 02 '15 at 23:27
3

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?

Community
  • 1
  • 1
Nathan
  • 10,593
  • 10
  • 63
  • 87
  • only real solution, since this was in a unit test was to have the text files in the test project as well – dinotom Nov 03 '15 at 08:51
-1

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
Simon Price
  • 3,011
  • 3
  • 34
  • 98