在c#中,如何使用登录用户启动新流程?
下面是场景在c#中,如何使用登录用户启动新流程?,c#,.net,C#,.net,下面是场景 使用用户名“JOHN”登录到windows 在c#中运行Windows应用程序Writern。此工具名为BootStrapper.exe。但我使用不同的用户“ALEX”使用RunAs特性执行了这个工具 引导程序将显示一些名为“启动应用程序”的按钮。单击启动时,使用c#的进程类执行Application.exe。请注意,我没有传递任何用户名和密码。因此Application.exe也在“ALEX”用户下运行 如何在Bootstrapper.exe的“JOHN”下运行Applicatio
请注意,Application.exe不知道“JOHN”的密码以模拟JOHN用户。在JOHN启动的进程中托管WCF服务(可能将其放入启动文件夹) 使用命令从ALEX进程调用WCF服务,告知要启动哪个进程。
从WCF服务启动该进程,它将以JOHN的身份运行。我为我的英语不好道歉。也许我误解了你。。。编译它,并将结果复制到“C:\test”目录。现在运行它
using System;
using System.Text;
using System.Diagnostics;
using System.Security;
using System.Reflection;
using System.IO;
namespace ConsoleApplication6 {
class Program {
unsafe static void Main(string[] args) {
Process process = new Process();
String dir = Path.GetDirectoryName(typeof(Program).Assembly.Location);
String txtFile = Path.Combine(dir, "example.txt");
if (!File.Exists(txtFile)) {
StreamWriter sw = File.CreateText(txtFile);
sw.Close();
sw.Dispose();
}
ProcessStartInfo info = new ProcessStartInfo();
info.Domain = "myDomainName";
info.UserName = "userName";
String pass = "userPassword";
fixed (char* password = pass) {
info.Password = new SecureString(password, pass.Length);
}
// Will be run notepad.exe
info.FileName = Environment.ExpandEnvironmentVariables(@"%winDir%\NOTEPAD.EXE");
// in notepad.exe will be open example.txt file.
info.Arguments = txtFile;
info.LoadUserProfile = false;
info.UseShellExecute = false;
info.WorkingDirectory = dir;
process.StartInfo = info;
process.Start();
}
}
}
关于您提到您将不知道JOHN的用户名/密码,但代码似乎有此项(或至少有此项的占位符)@zync我认为在没有指定密码(如果他有密码)的情况下,不可能代表其他用户启动进程。我也这么认为,但OP特别指出。我没有登录用户的密码。JOHN根本没有启动任何进程。这让我想到,我可以创建一个新的应用程序,该应用程序将始终在登录用户JOHN下运行,并在其上托管WCF服务,该服务将具有启动任何可执行文件的方法。谢谢