Search code examples
.netdirectoryrootcode-behind

get to root folder of website project from codebehind (.net)


I have a .net-website project and want to get some file from app_data folder.

I know I can get the folder in markup like this:

"~/App_Data/myfile.csv"

But from Code-behind, I tried

File.OpenWrite(String.Format(@"~/App_Data/myfile.csv"));

resulting in error

"Could not find path \"C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\~\App_Data\myfile.csv\" (translated)


Solution

  • The String.Format method doesn't do any URL conversion. Use the MapPath method to get the physical path of a virtual address:

    File.OpenWrite(Server.MapPath("~/App_Data/myfile.csv"))