You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics;
namespace ICSSoft.Frame.APP { public class ICSConnectSharedFolder { /// <summary>
/// 连接共享文件夹
/// </summary>
/// <param name="path">共享路径</param>
/// <param name="user">用户名</param>
/// <param name="pass">密码</param>
/// <returns></returns>
public static void linkFile(string path, string user, string pass) { string cLinkUrl = @"Net Use " + path + " " + pass + " /user:" + user; CallCmd(cLinkUrl); }
/// <summary>
/// 关闭所有共享连接
/// </summary>
public static void KillAllLink() { string cKillCmd = @"Net Use /delete * /yes"; CallCmd(cKillCmd); }
/// <summary>
/// 关闭指定连接
/// </summary>
/// <param name="path">共享路径</param>
public static void KillLink(string path) { string cKillCmd = @"Net Use " + path + " /delete /yes"; CallCmd(cKillCmd); }
/// <summary>
/// 调用Cmd命令
/// </summary>
/// <param name="strCmd">命令行参数</param>
private static void CallCmd(string strCmd) { //调用cmd命令
Process myProcess = new Process(); try { myProcess.StartInfo.FileName = "cmd.exe"; myProcess.StartInfo.Arguments = "/c " + strCmd; myProcess.StartInfo.UseShellExecute = false; //关闭Shell的使用
myProcess.StartInfo.RedirectStandardInput = true; //重定向标准输入
myProcess.StartInfo.RedirectStandardOutput = true; //重定向标准输出
myProcess.StartInfo.RedirectStandardError = true; //重定向错误输出
myProcess.StartInfo.CreateNoWindow = true; myProcess.Start(); } catch { } finally { myProcess.WaitForExit(); if (myProcess != null) { myProcess.Close(); } } }
/// <summary>
/// 连接远程共享文件夹
/// </summary>
/// <param name="path">远程共享文件夹的路径</param>
/// <param name="userName">用户名</param>
/// <param name="passWord">密码</param>
/// <returns></returns>
public static 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) { throw ex; } finally { proc.Close(); proc.Dispose(); } return Flag; } } }
|