7

I want to read a file path from the following structure

The Structure is like : AssemblyName -> MyFiles (Folder) -> Text.txt

Here I want to get the path of the Text.txt. Please help

user1238374
  • 83
  • 1
  • 2
  • 4

3 Answers3

7

You can do

string assemblyPath = Assembly.GetExecutingAssembly().Location;
string assemblyDirectory = Path.GetDirectoryName(assemblyPath);
string textPath = Path.Combine(assemblyDirectory, "MyFiles", "Test.txt");
string text = File.ReadAllText(textPath);

...just to split it up some...but you could write it all in one line needless to say...

alternatively, if your Environment.CurrentDirectory is already set to the directory of your executing assembly's location, you could just do

File.ReadAllText(Path.Combine("MyFiles", "Text.txt"));
Jeff
  • 35,755
  • 15
  • 108
  • 220
  • 1
    I am getting an Exception, "Could not find a part of the path 'c:\users\taks\documents\visual studio 2010\Projects\Console Applications\GetFilesFromAssembly\GetFilesFromAssembly\bin\Debug\MyFiles\Test.txt'" – user1238374 Feb 28 '12 at 17:12
  • 1
    @JeffN825 I think he's got an embedded file, not a file in the same directory as the assembly. – Eric Andres Feb 28 '12 at 17:22
  • 1
    This would not work if the file were in the library's folder instead of the executing assembly's folder. I have yet to find an approach that works for library sub folders except for the embedded resource approach. – user420667 Oct 26 '16 at 19:48
7

I think what you're looking for is a file embedded in the assembly. Check out this question. The first answer explains how to set up an embedded file, as well as how to get it from code.

Community
  • 1
  • 1
Eric Andres
  • 3,417
  • 2
  • 24
  • 40
1

Jeff has covered how you get the path, wrt your comment on his answer is the file you want to open actually included in your project output?

Under the properties pane for the relevant file look at the Copy to Output Directory option - it generally defaults to Do not copy. You will want to set it to Copy Always or Copy if Newer if you want to include a file in the output directory with your compiled program.

As a general note you should always wrap any IO in an appropriate try catch block or use the static File.Exists(path) method to check whether a file exists

RobV
  • 28,022
  • 11
  • 77
  • 119
  • 1
    The issue is I cannot copy this file to Output Directory, I have to access this file from the mentioned hierarchy. – user1238374 Feb 28 '12 at 17:19
  • 1
    thanks Eric for the suggestion, It is working, I have embedded the as a resource – user1238374 Feb 28 '12 at 17:29
  • 2
    @user1238374, what "mentioned hierarchy" is? Your first level is "AssemblyName -> " which everyone understands differently (embedded resource in the assembly, path relative to assembly, maybe even path relative to assembly in the GAC), please clarify what you mean (and possibly what you **actually** want to achieve). – Alexei Levenkov Feb 28 '12 at 17:33
  • 1
    @AlexeiLevenkov: Assembly contains a Folder name "MyFiles" which in turn contains a file name "Text.txt" . Now this assembly will be referred by another assembly in which I have to read the contain of "Text.txt" file. – user1238374 Feb 28 '12 at 17:36