AGuyCalledGerald
AGuyCalledGerald

Reputation: 8150

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)

Upvotes: 3

Views: 1035

Answers (1)

Guffa
Guffa

Reputation: 700342

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"))

Upvotes: 3

Related Questions