Ok, so I've been searching for a while and the title explains pretty much what I want to do. Also, there is no problem hard-coding the admin credentials in the code.
Initially I wrote some code in c# that ALMOST solved the problem:
private void button2_Click(object sender, EventArgs e)
{
DirectoryInfo myDirectoryInfo = new DirectoryInfo(textBox1.Text);
DirectorySecurity myDirectorySecurity = myDirectoryInfo.GetAccessControl();
string User = System.Environment.UserDomainName + "\\" + comboBox1.SelectedItem.ToString();
myDirectorySecurity.AddAccessRule(new FileSystemAccessRule(User, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow));
//myDirectorySecurity.AddAccessRule(new FileSystemAccessRule(User, FileSystemRights.Write, InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow));
myDirectoryInfo.SetAccessControl(myDirectorySecurity);
MessageBox.Show("Permissions Altered Successfully" + User);
}
This works just fine if I use on a folder that I already have permission, but what I need is a method that uses admin credentials to grant permission to a folder that a regular user don't have.
Later I tried to write some stuff in vbscript:
strHomeFolder = "C:\test"
strUser = " DOMAIN\user"
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "%COMSPEC% /c Echo Y| cacls "& strHomeFolder & " /e /c /g "& strUser &":F", 2, True
But i couldn't find a way to pass the admin credentials. So lastly I wrote another code to try to get it done:
private void button1_Click(object sender, EventArgs e)
{
try
{
//string passwordPre = "PASSWORD";
//char[] passwordChars = passwordPre.ToCharArray();
//SecureString password = new SecureString();
//foreach (char c in passwordChars)
//{
// password.AppendChar(c);
//}
ProcessStartInfo p = new ProcessStartInfo(@"D:\\test.vbs");
//p.UseShellExecute = false;
//p.UserName = "username";
//p.Domain = "DOMAIN";
//p.Password = password;
Process.Start(p);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
This time I just tried to pass admin credentials using a process, but it generated the message: The specified executable is not a valid application for this OS plataform.
So, is there any method that I can use to pass credentials? (Can be in c# or vbscript).
Thanks in advance.