brian4342
brian4342

Reputation: 1253

ASP.net Create a directory - Access denied

I am making an ASP.NET MVC application and I would like users to be able to save files to a folder.

I have this code:

if (!Directory.Exists("~/CSVfiles/" + userDetails.Username))
  Directory.CreateDirectory("~/CSVfiles/" + userDetails.Username);

So if the user does not have a folder in the directory specified then make one. Firstly is this a correct way to find a folder -> "~/CSVfiles/"

When it has created this folder I then want to be able to save a file to that folder I have this code:

System.IO.File.WriteAllLines("~/CSVfiles/" + userDetails.Username + "/doc.csv", CSV);

I have List CSV <- this is a list of strings comma separated.

I am currently getting this error:

System.UnauthorizedAccessException
{"Access to the path '~/CSVfiles/User1' is denied."}

Upvotes: 0

Views: 1684

Answers (2)

Directory is part of the common IO namespace and does not understand shorthand signs like '~' of ASP.NET. You have to convert it to some actual path... You may use Page.ResolveUrl or VirtualPathUtility...

Upvotes: 1

qamar
qamar

Reputation: 1447

Your asp.net users need to have permission on that folder. Check if asp.net app pool users have write access to your folder.

Upvotes: 0

Related Questions