使用WMI来控制Windows目录

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

代码如下:

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), null, null, 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
    }
}

 

如需转载,请注明本文原创自CSDN TJVictor专栏:http://blog.csdn.net/tjvictor

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值