Home > Net >  For help in c # RUNAS domain environment to run the program with the specified account
For help in c # RUNAS domain environment to run the program with the specified account

Time:10-07

 static void Main (string [] args) 
{

if (! RunAsAdmin (UserName, PassWord, the System Environment. The CurrentDirectory + "\ \ RunAS exe"))
{
Console. WriteLine (" run the main program error!" );
The Console. ReadKey ();
}
}
Public static bool RunAsAdmin (string inUserName, string inPassWord, string fileFullPath)
{
The Process MBRTProcess=null;

MBRTProcess=new Process ();
MBRTProcess. StartInfo. UserName=inUserName;
String strPWD=inPassWord;
SecureString password=new SecureString ();
The foreach (char c in strPWD. ToCharArray ())
{
Password. AppendChar (c);
}
MBRTProcess. StartInfo. Password=Password;
MBRTProcess. StartInfo. WorkingDirectory=System. Environment. CurrentDirectory;
MBRTProcess. StartInfo. FileName="RunAS. Exe";
MBRTProcess. StartInfo. UseShellExecute=false;
MBRTProcess. StartInfo. LoadUserProfile=true;
MBRTProcess. StartInfo. CreateNoWindow=true;
MBRTProcess. StartInfo. Domain="my Domain";
MBRTProcess. Start ();
return true;

}


Has been an error!
System.Com ponentModel. Win32Exception
HResult=0 x80004005
Message=directory name is invalid,
The Source=System
StackTrace:
In the System. Diagnostics. Process. StartWithCreateProcess (ProcessStartInfo startInfo)
In the System. Diagnostics. Process. The Start ()
In RunAsAdmin. Program. RunAsAdmin (String inUserName, String inPassWord, String fileFullPath) in C: \ Users \ yms \ source \ repos \ RunAS \ RunAsAdmin \ Program in cs: 43 line
In RunAsAdmin. The Program. The Main (String [] args) in C: \ Users \ yms \ source \ repos \ RunAS \ RunAsAdmin \ Program in cs: line 18

Main purpose is to a standard domain user to specify the domain administrator account to run certain programs, the results of the test is now, if this domain management account login the computer will not report this error, can normal to run a specified program, if change a didn't log in the computer domain administrator account, so the error "directory name is invalid", set the working directory is the main reason, may be not landed on domain account on this computer without the user configuration file, but don't know how to solve this problem, have a headache, want to half a day, thank you!
  •  Tags:  
  • C#
  • Related