During development, while you run the application as a console application or windows form application, often the code needs to load a specific file within the same folder of the exe assembly resides. The file can be located by using the file name directory since the default location will be the exe’s directory.
For instance,

File.Exists("myfile.xml");

However, it is a different story if you run the application as a WindowsService. In order to resolve the excuting assembly directory path, the code needs to do a bit extra work.

Console.WriteLine(Assembly.GetExecutingAssembly().Location) ;

result: “C:\FolderName\RunMe.exe”

Console.WriteLine(    Path.GetDirectoryName( Assembly.GetExecutingAssembly().Location ) );

result: “C:\FolderName”

So , load the specifc file “myfile.xml”

File.ReadAllText(    Path.GetDirectoryName(              Assembly.GetExecutingAssembly().Location  + @"\myfile.xml" ));

Tags: , , ,

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>