1

I want to create a folder on my current user's desktop folder, however; I keep getting an access denied message. I have full write permissions under my profile in IIS.

string activeDir = @"C:\Users\dmm\Desktop\";
string newPath = System.IO.Path.Combine(activeDir, "mySubDir");
System.IO.Directory.CreateDirectory(newPath);

Any help would be appreciated.

Alex K.
  • 171,639
  • 30
  • 264
  • 288
jan86
  • 107
  • 2
  • 15
  • Have you granted permissions to the application pool identity your app runs under? – Alex K. Dec 19 '16 at 11:48
  • Why is your web-service trying to create a folder on your users' desktop?? – Moo-Juice Dec 19 '16 at 11:52
  • have you tried running your application as **administrator** ? – Mong Zhu Dec 19 '16 at 11:54
  • Where would I find that under advanced settings on the Application Pool correct? – jan86 Dec 19 '16 at 11:54
  • This link should help you in the right direction if you wish to run app pools as administrator [Application Pools should be set to run as Application Pool Identities](https://technet.microsoft.com/en-us/library/dd378907(v=ws.10).aspx) . – Kraang Prime Dec 19 '16 at 12:27

4 Answers4

11

Try using the built in objects to get the desktop path, and let .NET also handle the path building for the new folder. You will also want to check if the directory exists first.

string newFolder = "abcd1234";

string path = System.IO.Path.Combine(
   Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
   newFolder
);

if(!System.IO.Directory.Exists(path)) {
   try {
      System.IO.Directory.CreateDirectory(path);
   } catch (IOException ie) {
      Console.WriteLine("IO Error: " + ie.Message);
   } catch (Exception e) {
      Console.WriteLine("General Error: " + e.Message);
   }
}
Kraang Prime
  • 9,981
  • 10
  • 58
  • 124
  • The folder isn't created on the desktop. – jan86 Dec 19 '16 at 12:16
  • @jan86 Set a break point on both `Console.WriteLine` lines, and then mouse over the word `Message` when it breaks on it. This should help you understand why you can't write to the desktop. Generaly speaking though, IIS disallows any user access such as System32, Desktop, StartMenu, and so on for security reasons. Setting up permissions to bypass this requires that you cascade premissions from all folders below. You are better off having IIS write to a database, and have a service or scheduled application read from that to execute commands. – Kraang Prime Dec 19 '16 at 12:19
  • @jan86 - as another option, you could write a simple socket server and have your webserver communicate with it locally to send commands. – Kraang Prime Dec 19 '16 at 12:23
2

When you deploy an application on IIS by default it is executed with ApplicationPoolIdentity. Which is virtual user created and named as IIS AppPool\YourPoolName If this virtual user does not have write access to your desktop. You get that exception.

You have two options.

  1. Give ApplicationPoolIdentity user write access to Desktop directory.

goto Desktop folder and add user IIS AppPool\YourPoolName with write access :

  1. Change pool Identity to user which has write access to directory. Go

IIS->Application Pools -> Your AppPool ->Advanced Settings -> Identity ->

Select Custom Account and click set button. and there you enter your windows user credentials.

I would recommend first option.

Davit Tvildiani
  • 1,915
  • 3
  • 19
  • 29
  • +1.I'd remove the last line though. Choosing between option 1 or 2 depends on the server admin. If you use the first option all Web Sites in the server using that AppPool would have access to the desktop directory. – hardkoded Dec 19 '16 at 12:09
  • I don't get the error anymore, however; the folder isn't being created. – jan86 Dec 19 '16 at 12:12
1

If you ran your logic from an IIS application, you should use Server.MapPath:

System.IO.Directory.CreateDirectory(Server.MapPath(newPath));
Ahmad Hosny
  • 597
  • 1
  • 6
  • 23
Andrei Filimon
  • 1,138
  • 8
  • 12
1

There are many to consider here, first of them being that your application is an ASP.NET application, and every current user will be different. If your application — just assume — runs correctly on your machine, it will never run on hosting environment because they do not grant write permissions to special folders and user accounts.

That said, you need to work in physical paths in order to create your directories.

var path = "C:\\Users\\afzaa\\Desktop\\";
var folder = Path.Combine(path, "folder");
Directory.CreateDirectory(folder);

The result of the above code is, enter image description here

As you can see, the code properly works and has no issue at all in execution.

There are few things to note:

  1. Your application has read/write permissions. Check IIS for that.
  2. Your code can actually lookup the path you are trying to access. That applies to any folder inside Desktop too, a sub folder may have special permissions applied.

Do not do this, write the content online in your hosting domain. Users have different accounts and structures for folders and thus this will not work — Desktop path is different.

If you want to users to download the file, simply stream the file down and let them save it where they want to.

https://forums.asp.net/t/1807775.aspx?Create+e+New+Folder+Access+Denied+ https://answers.microsoft.com/en-us/windows/forum/windows_xp-files/unable-to-create-the-folder-new-folder-access-is/ac318218-a7b2-4ee2-b301-2ad91856050b .NET MVC Access denied when trying to create Directory

Community
  • 1
  • 1
Afzaal Ahmad Zeeshan
  • 15,669
  • 12
  • 55
  • 103