C# .NET - How to create a folder in remote server in C#

Asked By Debarati Chattopadhyay on 09-Oct-08 11:58 AM


I have a requirement where I need to create a folder in a remote machine.

Can you please help me how to create that using C#.



solution - Perry replied to Debarati Chattopadhyay on 09-Oct-08 01:03 PM


Use below code:Make sure that you have write permission on remote location you have specified. This example also creates a file under that folder.

public class CreateFileOrFolder
    static void Main()
        string folderName = "\\\\domain\\site\\folderlocation\\foldername";


        // Create a new file name. This example generates
        // a random string.
        string newFileName = System.IO.Path.GetRandomFileName();

        // Combine the new file name with the path
        newPath = System.IO.Path.Combine(folderName, newFileName);




create a folder in remote server in C# - Binny ch replied to Debarati Chattopadhyay on 09-Oct-08 01:35 PM

string path= @ "\\servername or IPAddress\\foldername \\"


Please make sure that the shared folder in remote machine underwhich you are creating your new folder has permissions for you

re - Web Star replied to Debarati Chattopadhyay on 10-Oct-08 12:26 AM

You'll need to be running with credentials the remote server
understands and can authenticate.

One way to do this is to run the http://www.velocityreviews.com/forums/t85011-create-folder-in-remote-server.html#.NET worker process under a http://www.velocityreviews.com/forums/t85011-create-folder-in-remote-server.html#
account with permissions on the remote server.

public string[] GetFileList()
    string[] downloadFiles;
    StringBuilder result = new StringBuilder();
    FtpWebRequest reqFTP;
        reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(
                  "ftp://" + ftpServerIP + "/"));
        reqFTP.UseBinary = true;
        reqFTP.Credentials = new NetworkCredential(ftpUserID,
        reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
        WebResponse response = reqFTP.GetResponse();
        StreamReader reader = new StreamReader(response
        string line = reader.ReadLine();
        while (line != null)
            line = reader.ReadLine();
        // to remove the trailing '\n'
        result.Remove(result.ToString().LastIndexOf('\n'), 1);
        return result.ToString().Split('\n');
    catch (Exception ex)
        downloadFiles = null;
        return downloadFiles;

try this - C_A P replied to Debarati Chattopadhyay on 10-Oct-08 07:54 AM
You can use the following code:

using System.Security.Principal;

NTAccount ntAccount = new NTAccount("Everyone"); // or whatever account you
will grant access
SecurityIdentifier sid = (SecurityIdentifier)ntAccount
byte[] sidArray = new byte[sid.BinaryLength];
sid.GetBinaryForm(sidArray , 0);
ManagementObject Trustee = new ManagementClass(new
ManagementPath("Win32_Trustee"), null);
Trustee["SID"] = sidArray ;

When specifying the SID this way, neither the ["Domain"] nor the ["Name"]
properties are needed. I have not experienced that the ["SIDLength"] is ever

If the NTAccount constructor is not specifying a domain, the SAM on the
local system will be used.
try this - C_A P replied to Debarati Chattopadhyay on 10-Oct-08 07:56 AM


If you have set "Full Control" in security tab of your share folder, then Add the FileSystemAccessRule to the security settings like this

    string path = @"\\\New\new2"; //I assume that: Your file server ip is
//you have shared the "New" folder and set Full control, then you want to create "new2" folder and add domain account for "new2" ACL

if (Directory.Exists(path))
DirectoryInfo di = Directory.CreateDirectory(path);

DirectoryInfo dInfo = new DirectoryInfo(path);

// Get a DirectorySecurity object that represents the

// current security settings.

DirectorySecurity dSecurity = dInfo.GetAccessControl();

string domainName = "mydomain";
string identity = "myaccount";

// Add the FileSystemAccessRule to the security settings.

FileSystemAccessRule fsr = new FileSystemAccessRule(domainName + @"\" + identity,

System.Security.AccessControl.FileSystemRights.ReadData |

System.Security.AccessControl.FileSystemRights.WriteData |




// Set the new access settings.

Creating folder in a remote machine - Mahendra Gupta replied to Debarati Chattopadhyay on 21-May-09 07:16 AM
end of post