0

I want to create a folder on remote machine using remote machine administrator credentials, using Directory.CreateDirectory how to pass user name and password for creating directory.

N.RaviKumar
  • 275
  • 1
  • 5
  • 14
  • 1
    Do you have access to the remote machine? As in can you do remote login? – Pratik Gaikwad Dec 05 '16 at 03:02
  • As @PratikGaikwad suggest, first you need to know if have access to that folder, and if you are granted to write into it. – Facundo La Rocca Dec 05 '16 at 03:04
  • If you have remote access then you have to look take a look at [ConnectionOptions](https://msdn.microsoft.com/en-us/library/system.management.connectionoptions(v=vs.110).aspx?f=255&MSPPError=-2147217396&cs-save-lang=1&cs-lang=csharp#code-snippet-2) class – Mahesh Dec 05 '16 at 03:06
  • @PratikGaikwad, yes I m able to remote login to machine, I have the admin user name and password. – N.RaviKumar Dec 05 '16 at 04:27
  • @FacundoLaRocca, since I m creating a new folder in c- drive, I think permission is not required – N.RaviKumar Dec 05 '16 at 04:28

2 Answers2

1

First check if you have access to the folder and if are allowed to write. If you have, then use this way:

var directorypath = @"\\172.16.136.35\SharedFolder1\";
(!Directory.Exists(directorypath))
{
    Directory.CreateDirectory(directorypath);
}

Where "172.16.136.35" must be replaced by the remote computer ip or dns. Take a look at this post

Now, if what you want is executing as admin, you might declare it in the manifest, see this post

The code below was published in the same post and lets you check if you are running as admin or you dont.

using System.Security.Principal;
public bool IsUserAdministrator()
{
    bool isAdmin;
    try
    {
        WindowsIdentity user = WindowsIdentity.GetCurrent();
        WindowsPrincipal principal = new WindowsPrincipal(user);
        isAdmin = principal.IsInRole(WindowsBuiltInRole.Administrator);
    }
    catch (UnauthorizedAccessException ex)
    {
        isAdmin = false;
    }
    catch (Exception ex)
    {
        isAdmin = false;
    }
    return isAdmin;
}

Really, I dont know whether is a programmatic way to force an application to elevate its own perms. In my experience, the app should be elevated at begining. Lets take an example:

  • command prompt: many times we find programs, process, or whatever, which needs to be installed as Admin, if I opened cmd as a common user, usually it throws an exception. Then you have to close cmd and open it again as admin, right?

I could think in many security risks if I was some Operating System and I allowed to anyone to elevate itself as admin.

I hope you to solve the problem.

Community
  • 1
  • 1
Facundo La Rocca
  • 3,786
  • 2
  • 25
  • 47
1

If you have access on remote machine then you can use tcp/ip protocol to create the folder directly using below code:

string _directoryName = @"\\ServerIP\C$\FolderName";
(!Directory.Exists(_directoryName))
{   
    Directory.CreateDirectory(_directoryName);
}

however if you want to create the folder using admin user only with specific permission you can use psexec to create the folder. I am assuming you know how to invoke external exe or process from c#. You can pass below command argument to psexec for creating the folder

psexec \\\IPAddress -u username -p Password cmd /c mkdir c:\FolderName
Pratik Gaikwad
  • 1,526
  • 2
  • 21
  • 44
  • 2
    where are you actually pointing to the psexec.exe your post assumes that the file is installed locally, but what if I want to run it with it stored in a applications Resource folder..? – MethodMan Nov 16 '18 at 21:57