
Untitled
By: a guest on
May 7th, 2012 | syntax:
None | size: 1.55 KB | hits: 22 | expires: Never
Creating Windows Folders using impersonation
//set the cursor
string activeDir = "\\department\shares\users\";
//create directory with userID as the folder name
string newPath = System.IO.Path.Combine(activeDir + userID);
System.IO.Directory.CreateDirectory(newPath);
using (var impersonation = new ImpersonatedUser(decryptedUser, decryptedDomain, decryptedPassword))
{
Directory.CreateDirectory(newPath);
}
public class WNet
{
public static void AddConnection(string resource, string username, string password)
{
NETRESOURCE nr = new NETRESOURCE();
nr.RemoteName = resource;
uint err = WNetAddConnection2W(ref nr, password, username, 0);
if (err != 0)
throw new RemoteDirectoryException(string.Format("WNetAddConnection2 failed with error: #{0}", err));
}
private struct NETRESOURCE
{
public uint Scope;
public uint Type;
public uint DisplayType;
public uint Usage;
public string LocalName;
public string RemoteName;
public string Comment;
public string Provider;
}
[DllImport("mpr.dll", CharSet = CharSet.Unicode)]
private extern static uint WNetAddConnection2W(ref NETRESOURCE lpNetResource, string lpPassword, string lpUsername, uint dwFlags);
}
string activeDir = "\\department\shares\users\";
string username = "username";
string password = "password";
WNet.AddConnection(activeDir, username, password);
string newPath = System.IO.Path.Combine(activeDir, userID);
System.IO.Directory.CreateDirectory(newPath);