DotnetSparrow
DotnetSparrow

Reputation: 27996

file path using C#

I have a windows service which is using a method from a class library with same asp.net solution. in class library, I have a method with following line:

reader = XmlReader.Create(HttpContext.Current.Server.MapPath("~/TestDevice/Data.xml"), settings);

When control comes to this line. I get exception. I tried to debug the code and found that when service tries to access this method then HttpContext.Current.Server is null. What is alternative syntax.

I tried to access this class library method from web application and it works fine.

System.IO.Path.GetFullPath("/TestDevice/Data.xml") returns C:\\TestDevice\\Data.xml instead of the actual directory path

I want to get full path of the folder.

Please suggest solution.

Upvotes: 1

Views: 2596

Answers (3)

Joel Etherton
Joel Etherton

Reputation: 37523

You will need a configuration file that can have the "root" directory set specifically. This will allow the windows service to know what directory to place files into regardless of where its executable sits and regardless of where the asp.net site is configured to run.

Upvotes: 2

toby
toby

Reputation: 902

I don't think the ~ will work in this case, you will need to provide a relative path. Something like "../../TestDevice/Data.xml" should work.

Upvotes: 0

Joel Martinez
Joel Martinez

Reputation: 47751

http://msdn.microsoft.com/en-us/library/aa457089.aspx

string path;
path = System.IO.Path.GetDirectoryName( 
  System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );

Upvotes: 3

Related Questions