首页 > 代码库 > c# 设置和取消文件夹共享及执行Dos命令
c# 设置和取消文件夹共享及执行Dos命令
/// <summary> /// 设置文件夹共享 /// </summary> /// <param name="FolderPath">文件夹路径</param> /// <param name="ShareName">共享名</param> /// <param name="Description">共享注释</param> /// <returns></returns> public static int ShareNetFolder(string FolderPath, string ShareName, string Description) { try { ManagementClass managementClass = new ManagementClass("Win32_Share"); // Create ManagementBaseObjects for in and out parameters ManagementBaseObject inParams = managementClass.GetMethodParameters("Create"); ManagementBaseObject outParams; // Set the input parameters inParams["Description"] = Description; inParams["Name"] = ShareName; inParams["Path"] = FolderPath; inParams["Type"] = 0x0; // Disk Drive outParams = managementClass.InvokeMethod("Create", inParams, null); // Check to see if the method invocation was successful if ((uint)(outParams.Properties["ReturnValue"].Value) != 0) { throw new Exception("Unable to share directory."); } //设置所有人可读 DirectorySecurity fSec = new DirectorySecurity(); fSec.AddAccessRule(new FileSystemAccessRule(@"Everyone", FileSystemRights.FullControl, AccessControlType.Allow)); System.IO.Directory.SetAccessControl(FolderPath, fSec); } catch { return -1; } return 0; } /// <summary> /// 取消文件夹共享 /// </summary> /// <param name="ShareName">文件夹的共享名</param> /// <returns></returns> public static int CancelShareNetFolder(string ShareName) { try { SelectQuery selectQuery = new SelectQuery("Select * from Win32_Share Where Name = ‘" + ShareName + "‘"); ManagementObjectSearcher searcher = new ManagementObjectSearcher(selectQuery); foreach (ManagementObject mo in searcher.Get()) { mo.InvokeMethod("Delete", null, null); } } catch { return -1; } return 0; } /// <summary> /// 执行Dos命令并返回执行结果,默认2秒内执行完 /// /// net session /delete /y 删除该计算机的所有会话(局域网服务器连接超最大数时刻执行) /// 如果是服务器部署了网站,删除会话命令慎用 /// </summary> /// <param name="dosCommand">dos命令</param> /// <param name="milliseconds">最大执行秒数</param> /// <returns></returns> public static string Execute(string dosCommand, int milliseconds=2000) { string output = ""; //输出字符串 if (dosCommand != null && dosCommand != "") { Process process = new Process(); //创建进程对象 ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.FileName = "cmd.exe"; //设定需要执行的命令 startInfo.Arguments = "/C " + dosCommand; //设定参数,其中的“/C”表示执行完命令后马上退出 startInfo.UseShellExecute = false; //不使用系统外壳程序启动 startInfo.RedirectStandardInput = false; //不重定向输入 startInfo.RedirectStandardOutput = true; //重定向输出 startInfo.CreateNoWindow = true; //不创建窗口 process.StartInfo = startInfo; try { if (process.Start()) //开始进程 { if (milliseconds == 0) process.WaitForExit(); //这里无限等待进程结束 else process.WaitForExit(milliseconds); //这里等待进程结束,等待时间为指定的毫秒 output = process.StandardOutput.ReadToEnd();//读取进程的输出 } } catch { } finally { if (process != null) process.Close(); } } return output; }
c# 设置和取消文件夹共享及执行Dos命令
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。