I have a service that is hosted in SharePoint. What I want to do is to access file system in the service, say create directories and files on the server (Web front end server). The current logged in user does not have permission on the file system and I don't want to give them permission.

What I am looking for is to do System.IO operations under application pool account. something like SharePoint's RunWithElevatedPrivileges but for IO operations. More specifically it is System.IO.Directory.CreateDirectory(string) and also System.IO.File.WriteAllBytes(string,byte[])

Is there a way to elevate privileges for these sorts of operations?


You can use RunWithElevatedPrivileges to perform System.IO operations.

Try below code:

    String folderpath = "" + @"c:\myfolder";
    if (!System.IO.Directory.Exists(folderpath))

    String timestamp = Utility.GetTimestamp(DateTime.Now);

    String fileName = "test"+ "_" + timestamp + ".txt";
    string filePath = folderpath + "\\" + fileName;

    System.IO.File.WriteAllText(filePath, "Test");

Reference - Cannot write file in a folder

| improve this answer | |
  • I just tried it and yes right, I thought RunWithElevatedPrivileges only works in SharePoint Context. Thanks! – Paiman Samadian Dec 8 '16 at 23:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.