Reputation: 2413
I am trying to create a safe file transfer system and in my file server I have a webservice which receives an array of files and will create some directories and subdirectories according to parameters like the users username who has uploaded the files and datetime of file uploading . So here is the issue :
1 - I have to create folders and sub folders dynamically and per request and I am able to create folders
2 - I want to save each file in It's directory after creating the mentioned folders
3 - When I try to save files using following code I get the Access is denied
error message
string RootSavePath = @"C:\SFTFileSharingFolder";
string RequestSavePath = RootSavePath + "\\Date " + getShamsiDate().Replace("/", "-") + " Time " + DateTime.Now.Hour.ToString() + "." + DateTime.Now.Minute.ToString() + "." + DateTime.Now.Second.ToString();
if (!Directory.Exists(RequestSavePath))
{
Directory.CreateDirectory(RequestSavePath);
}
fl.SaveAs(RequestSavePath);
fl is Id of the FileUpload Control
What I want to know :
Is there any way to give permission to current user and application on dynamically created folders from c# code behind So the files would be saved with no error ?
Upvotes: 1
Views: 2745
Reputation: 63065
Give modification rights for the parent directory on the drive which you create the folders to user who running your web application/ app pool
string RootSavePath = @"C:\SFTFileSharingFolder";
var serverPath = Path.Combine(RootSavePath , "Date " + getShamsiDate().Replace("/", "-") +
" Time " + DateTime.Now.Hour.ToString() + "." +
DateTime.Now.Minute.ToString() + "." +
DateTime.Now.Second.ToString());
if (!Directory.Exists(serverPath))
{
Directory.CreateDirectory(serverPath);
}
fl.SaveAs(Path.Combine(serverPath, f1.FileName);
Upvotes: 0
Reputation: 33139
Assuming that your Web application runs under a certain account (and not doing impersonation), all you need to do is grant that account Full Access to the folder where your code will be creating subfolders.
If you are using impersonation, that means the code runs under the account of the authenticated user. In that case, you have to grant all users access to that folder -- or rather, the Active Directory group that you use for authorizing access.
Upvotes: 1
Reputation: 2030
I think because the IIS server was not configured correctly. I think there is your place to find out an answer Setting up permissions to allow ASP.net code behind to create folders on Server
Upvotes: 1