使用WMI来控制Windows目录

本文主要介绍如何使用WMI来查询目录是否存在、文件是否存在、如何建立目录、删除目录,删除文件、如何利用命令行拷贝文件,如何利用WMI拷贝文件

代码如下:

+展开
-C#
using System;
using System.Collections.Generic;
using System.Text;
using System.Management;
using System.Threading;
using System.Diagnostics;

namespace TJVictor.WMI
{
    public class Win32_Directory : WMIBaseClass
    {
        #region Property
        private int timeout = 30;
        public int TimeOut
        {
            get { return timeout; }
            set { timeout = value; }
        }
        #endregion

        #region Construction
        public Win32_Directory()
            : base()
        {
            base.Connection();
        }
        public Win32_Directory(string domain, string Ip, string user, string psw)
            : base(domain, Ip, user, psw)
        {
            base.Connection();
        }
        #endregion

        #region public function
        public bool IsDirExist(string path)
        {
            string wqlSelect = "select * FROM Win32_Directory where Name='{0}'";
            if (!GetSelectQueryCollection(wqlSelect, path.Replace("\\", "\\\\")).Count.Equals(0))
                return true;
            return false;
        }

        public bool IsFileExist(string path)
        {
            string wqlSelect = "select * FROM CIM_DataFile where Name='{0}'";
            if (!GetSelectQueryCollection(wqlSelect, path.Replace("\\", "\\\\")).Count.Equals(0))
                return true;
            return false;
        }

        public void CreateDir(string path)
        {
            if (IsDirExist(path))
            {
                throw new TJVictor.WMI.WmiException.DirectoryException(string.Format("无法创建 {0}, 目录已经存在",path));
            }
            ManagementClass processClass = new ManagementClass("Win32_Process");
            processClass.Scope = base.Scope;
            object[] methodArgs = { string.Format("cmd.exe /c md {0}", path), nullnull, 0 };
            // Method Options
            object result = processClass.InvokeMethod("Create", methodArgs);
            CheckExceptionClass.CheckDirectoryExcepton(int.Parse(result.ToString()));
        }

        public void DeleteDir(string path)
        {
            if (!IsDirExist(path))
                throw new TJVictor.WMI.WmiException.DirectoryException(string.Format("无法删除 {0}, 目录不存在",path));
            string wqlSelect = "select * FROM Win32_Directory where Name='{0}'";
            object result = 0;
            foreach (ManagementObject mo in GetSelectQueryCollection(wqlSelect, path.Replace("\\", "\\\\")))
            {
                result = mo.InvokeMethod("Delete"null);
                break;
            }
            CheckExceptionClass.CheckDirectoryExcepton(int.Parse(result.ToString()));
        }

        public void DeleteFile(string flieName)
        {
            if (!IsFileExist(flieName))
                throw new TJVictor.WMI.WmiException.DirectoryException(string.Format("{0} 文件不存在",flieName));
            string wqlSelect = "select * FROM CIM_DataFile where Name='{0}'";
            object result = 0;
            foreach (ManagementObject mo in GetSelectQueryCollection(wqlSelect, flieName.Replace("\\", "\\\\")))
            {
                result = mo.InvokeMethod("Delete"null);
                break;
            }
            CheckExceptionClass.CheckDirectoryExcepton(int.Parse(result.ToString()));
        }

        public void CopyDirByProcess(string oldDirPath, string newDirPath)
        {
            int tempTimeOut = this.timeout;
            Process copyProcess = new Process();
            copyProcess.StartInfo.CreateNoWindow = true;
            copyProcess.StartInfo.UseShellExecute = false;
            copyProcess.StartInfo.FileName = string.Format(@"C:\WINDOWS\system32\xcopy.exe");
            copyProcess.StartInfo.Arguments = string.Format(@"/e /y {0} {1}", oldDirPath, newDirPath);
            copyProcess.Start();
            while (!copyProcess.HasExited)
            {
                Thread.Sleep(1000);
                copyProcess.Refresh();
                if (tempTimeOut.Equals(0))
                    throw new WmiException.DirectoryException(string.Format("从 {0} 到 {1} 复制文件失败--{2}秒超时", oldDirPath, newDirPath, this.timeout));
                tempTimeOut--;
            }
        }

        public void CopyDirByWmi(string oldDirPath, string newDirPath)
        {
            int tempTimeOut = this.timeout;
            ManagementClass processClass = new ManagementClass("Win32_Process");
            processClass.Scope = base.Scope;

            ManagementBaseObject mbo = processClass.GetMethodParameters("Create");
            mbo["CommandLine"] = string.Format("xcopy.exe /e /y {0} {1}", oldDirPath, newDirPath + "\\*.*");
            ManagementBaseObject ob = processClass.InvokeMethod("Create", mbo, null);

            string wqlSelect = "select * FROM Win32_Process where ProcessID='{0}'";
            while (!GetSelectQueryCollection(wqlSelect, ob["ProcessID"].ToString()).Count.Equals(0))
            {
                if (tempTimeOut.Equals(0))
                {
                    throw new WmiException.DirectoryException(string.Format("从 {0} 到 {1} 拷贝文件失败----超时", oldDirPath, newDirPath));
                }
                tempTimeOut--;
                Thread.Sleep(1000);
            }
        }
        #endregion
    }
}

加支付宝好友偷能量挖...


评论(0)网络
阅读(164)喜欢(0)Asp.Net/C#/WCF