zoukankan      html  css  js  c++  java
  • C#远程共享文件路径访问

     public class Win32ServiceManager
        {
            private string strPath;
            private ManagementClass managementClass;
            public Win32ServiceManager()
                : this(".", null, null)
            {
            }
            /// <summary>
            /// 远程连接建立
            /// </summary>
            /// <param name="host"></param>
            /// <param name="userName"></param>
            /// <param name="password"></param>
            public Win32ServiceManager(string host, string userName, string password)
            {
                this.strPath = "\\" + host + "\root\cimv2:Win32_Service";
                this.managementClass = new ManagementClass(strPath);
                if (userName != null && userName.Length > 0)
                {
                    ConnectionOptions connectionOptions = new ConnectionOptions();
                    connectionOptions.Username = userName;
                    connectionOptions.Password = password;
                    ManagementScope managementScope = new ManagementScope("\\" + host + "\root\cimv2", connectionOptions);
                    this.managementClass.Scope = managementScope;
                }
            }
            // 验证是否能连接到远程计算机
            public static bool RemoteConnectValidate(string host, string userName, string password)
            {
                ConnectionOptions connectionOptions = new ConnectionOptions();
                connectionOptions.Username = userName;
                connectionOptions.Password = password;
                ManagementScope managementScope = new ManagementScope("\\" + host + "\root\cimv2", connectionOptions);
                try
                {
                    managementScope.Connect();
                }
                catch
                {
                }
                return managementScope.IsConnected;
            }
            // 获取指定服务属性的值
            public object GetServiceValue(string serviceName, string propertyName)
            {
                ManagementObject mo = this.managementClass.CreateInstance();
                mo.Path = new ManagementPath(this.strPath + ".Name="" + serviceName + """);
                return mo[propertyName];
            }
            // 获取所连接的计算机的所有服务数据
            public string[,] GetServiceList()
            {
                string[,] services = new string[this.managementClass.GetInstances().Count, 4];
                int i = 0;
                foreach (ManagementObject mo in this.managementClass.GetInstances())
                {
                    services[i, 0] = (string)mo["Name"];
                    services[i, 1] = (string)mo["DisplayName"];
                    services[i, 2] = (string)mo["State"];
                    services[i, 3] = (string)mo["StartMode"];
                    i++;
                }
                return services;
            }
            // 获取所连接的计算机的指定服务数据
            public string[,] GetServiceList(string serverName)
            {
                return GetServiceList(new string[] { serverName });
            }
            // 获取所连接的计算机的的指定服务数据
            public string[,] GetServiceList(string[] serverNames)
            {
                string[,] services = new string[serverNames.Length, 4];
                ManagementObject mo = this.managementClass.CreateInstance();
                for (int i = 0; i < serverNames.Length; i++)
                {
                    mo.Path = new ManagementPath(this.strPath + ".Name="" + serverNames[i] + """);
                    services[i, 0] = (string)mo["Name"];
                    services[i, 1] = (string)mo["DisplayName"];
                    services[i, 2] = (string)mo["State"];
                    services[i, 3] = (string)mo["StartMode"];
                }
                return services;
            }
            // 停止指定的服务
            public string StartService(string serviceName)
            {
                string strRst = null;
                ManagementObject mo = this.managementClass.CreateInstance();
                mo.Path = new ManagementPath(this.strPath + ".Name="" + serviceName + """);
                try
                {
                    if ((string)mo["State"] == "Stopped")//!(bool)mo["AcceptStop"]
                        mo.InvokeMethod("StartService", null);
                }
                catch (ManagementException e)
                {
                    strRst = e.Message;
                }
                return strRst;
            }
        }

  • 相关阅读:
    Air-WIFI使用方法
    libeasy代码学习:一次简单的TCP请求处理流程
    log_pipe日志收集分析系统介绍
    asp.net 项目在 IE 11 下出现 “__doPostBack”未定义 的解决办法
    用命令行实现“一键休眠”
    C#几个经常用到的字符串的截取
    MYSQL存储过程怎么写
    如何用myeclipse和eclipse搭建安卓开发环境
    C#随机生成不重复字符串
    Inno setup 简单打包教程
  • 原文地址:https://www.cnblogs.com/jeffry/p/5749636.html
Copyright © 2011-2022 走看看