Reputation: 159
I know that when you're using ASP.NET, you can use runat="server" to get relative pathing to work. Is there a way I can accomplish that with a regular string path? My call looks like this
userConfig.Load("~\\App_Data\\userConfigDefault.xml");
I get the error code of
Could not find a part of the path 'C:\Program Files (x86)\IIS Express\~\App_Data\userConfigDefault.xml'.
I'd the xml file to be in my solution, but I also can't hard code its location because this application is going to be portable. I need to figure out how to relative path in my C# code.
Any help would be great. Thanks.
Edit: The file path for the XML file I'm trying to read is actually the App_Data folder in the project solution. Rion's first solution pointed me to my user\AppData\Roaming. I am not a picky man. Pointing to any folder in my Visual Studio solution will solve my problem.
Upvotes: 1
Views: 655
Reputation: 76607
Generally if you are going to access a file from your App_Data directory, you should consider using the Environment.SpecialFolder.ApplicationData
property as seen below :
// This will resolve the appropriate path for your App_Data directory
var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData),"userConfigDefault.xml");
Alternatively, Server.MapPath()
will also work and should accept a relative path that points to the appropriate directory :
var path = Server.MapPath("~/App_Data/"userConfigDefault.xml");
Upvotes: 2