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

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

Hi

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#.

Thanks

Debarati


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

Hi,

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";

         System.IO.Directory.CreateDirectory(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);

        }
}

Regards,

Megha

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 \\"

System.IO.Directory.CreateDirectory(path);

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;
    try
    {
        reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(
                  "ftp://" + ftpServerIP + "/"));
        reqFTP.UseBinary = true;
        reqFTP.Credentials = new NetworkCredential(ftpUserID,
                                                   ftpPassword);
        reqFTP.Method = WebRequestMethods.Ftp.ListDirectory;
        WebResponse response = reqFTP.GetResponse();
        StreamReader reader = new StreamReader(response
                                        .GetResponseStream());
       
        string line = reader.ReadLine();
        while (line != null)
        {
            result.Append(line);
            result.Append("\n");
            line = reader.ReadLine();
        }
        // to remove the trailing '\n'
        result.Remove(result.ToString().LastIndexOf('\n'), 1);
        reader.Close();
        response.Close();
        return result.ToString().Split('\n');
    }
    catch (Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message);
        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
..Translate(typeof(SecurityIdentifier));
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
needed.

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

Hi

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 = @"\\192.168.115.27\New\new2"; //I assume that: Your file server ip is 192.168.115.27
//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))
{
return;
}
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 |

System.Security.AccessControl.FileSystemRights.Modify,

AccessControlType.Allow);

dSecurity.AddAccessRule(fsr);

// Set the new access settings.

dInfo.SetAccessControl(dSecurity);
Creating folder in a remote machine - Mahendra Gupta replied to Debarati Chattopadhyay on 21-May-09 07:16 AM
end of post