码迷,mamicode.com
首页 > Windows程序 > 详细

c# 设置和取消文件夹共享及执行Dos命令

时间:2017-05-26 19:01:33      阅读:305      评论:0      收藏:0      [点我收藏+]

标签:最大   direct   文件夹路径   ssi   end   删除   argument   ams   shell   

        /// <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命令

标签:最大   direct   文件夹路径   ssi   end   删除   argument   ams   shell   

原文地址:http://www.cnblogs.com/lcawen/p/6909929.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!