2014-09-13 61 views
0

我想根据这张图片将密码放在正确的位置。运行命令和输入参数失败

我不知道如何把它放在那里,请告知

我用这个方法来启动CMD LINK

,我用这个方法发送的命令......

// start process 
process.Start(); 
// send command to its input 
using (StreamWriter sw = process.StandardInput) 
    { 
     if (sw.BaseStream.CanWrite) 
     { 
       sw.WriteLine("C:\\Windows\\System32\\runas.exe /user:xxxxxx\\xxxxx \"C:\\xxxx\\xxx.exe\""); 
       sw.WriteLine("password"); 
       Console.ReadKey(); 
     } 
    } 
    //wait 
    process.WaitForExit(); 

enter image description here

+0

我认为诀窍在于使用Console.Write()代替Console.WriteLine()作为提示文本,并且不要使用\ r \ n作为您写入的最后一个字符。 – RenniePet 2014-09-13 08:12:03

+0

当您的程序输入“密码”时,似乎进程已经终止。请参阅“1326:用户名或密码不正确” - 是否可以在此行完成后,将“password”键入标准stdout(在本例中为cmd)? – 2014-09-13 08:13:19

+0

这并不欺骗 – 2014-09-13 08:29:09

回答

2

您可以提供UserName,PasswordDomainProcessStartInfo运行exe文件在不同的凭据

注意那必须是一个SecureString

process.StartInfo.UserName ="username"; 
process.StartInfo.Domain = "domain"; 

process.StartInfo.FileName = "YourExeFile.exe"; 
process.StartInfo.WorkingDirectory = @"c:\path\where\exe\is\"; 
process.StartInfo.UseShellExecute = false; 

// password  
char[] chars = "password".ToCharArray(); 
// Instantiate a new secure string. 
fixed(char* pChars = chars) 
{ 
    process.StartInfo.Password = new SecureString(pChars, chars.Length); 
} 

process.Start(); 

有了这个就没有必要使用runas方式,摆弄容易出错的输入和OutputStream处理密码。

+0

我已经5个小时左右再次阅读您的解决方案。最后我可以知道并理解如何使用它thx! @rene – 2014-09-13 16:48:21