1.登录代码/// summary/// 登录权限/// /summary/// param name=path/param/// param name=userName/param/// param name=passWord/param/// returns/returnspublic bool ConnectState(s...
1.登录代码
/// <summary>
/// 登录权限
/// </summary>
/// <param name="path"></param>
/// <param name="userName"></param>
/// <param name="passWord"></param>
/// <returns></returns>
public bool ConnectState(string path, string userName, string passWord)
{
bool Flag = false;
Process proc = new Process();
try
{
proc.StartInfo.FileName = "cmd.exe";
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
string dosLine = "net use " + path + " " + passWord + " /user:" + userName;
proc.StandardInput.WriteLine(dosLine);
proc.StandardInput.WriteLine("exit");
while (!proc.HasExited)
{
proc.WaitForExit(1000);
}
string errormsg = proc.StandardError.ReadToEnd();
proc.StandardError.Close();
if (string.IsNullOrEmpty(errormsg))
{
Flag = true;
}
else
{
throw new Exception(errormsg);
}
}
catch (Exception ex)
{
DisplaylistboxMsg(ex.Message);
}
finally
{
proc.Close();
proc.Dispose();
}
return Flag;
}
2.执行登录
private void 登录_Click(object sender, EventArgs e)
{
bool userbool = ConnectState(@"\\**.**.*.*",User.Text,Pwd.Text);
if (userbool)
{
DisplaylistboxMsg("登录成功:"+User.Text);
}
else
{
DisplaylistboxMsg("登录失败");
}
}
沃梦达教程
本文标题为:c# 执行windows模拟登录
基础教程推荐
猜你喜欢
- c#-使用Shell API重命名映射的驱动器 2023-11-28
- 几分钟搞懂c#之FileStream对象读写大文件(推荐) 2023-01-22
- C#纯代码实现打字游戏 2023-02-17
- C#设计模式之适配器模式与装饰器模式的实现 2023-06-05
- c#-串口通信-进程间-简单实验 2023-11-24
- C#面向对象设计原则之开闭原则 2023-05-25
- C#对Word文档的创建、插入表格、设置样式等操作实例 2022-12-11
- C#简单了解接口(Interface)使用方法 2023-03-10
- C#生成带二维码的专属微信公众号推广海报实例代码 2023-01-06
- C#多线程实现异步接口 2023-05-26