【分享】采用CMD/WMI获取共享文件夹——及2种方式的性能对比

  之前在网上看到有朋友写了篇关于“如何用C#获取本地计算机共享文件夹”的文章,看了下代码用的是WMI方式,也就是调用System.Management中的类和方法,来获取计算机共享文件夹。我记得自己几年前有个项目需要获取硬件信息,当时用的也是WMI方式,留给自己的印象是WMI挺慢的。所以就动手写了个测试,发现WMI方式获取共享文件夹其时并不慢,也许只是获取某些特定硬件信息时才慢吧。

 

  我写的测试示例,包含两个测试,一种是用CMD方式,另一种是WMI方式,我的测试结果是CMD比WMI方式要慢一些,毕竟启动线程是要花时间的。其中WMI方式大家应该都懂,CMD方式是使用C#调用cmd.exe并接收命令行所返回的信息,如果需要做PING或调用控件台命令时,可能会用到,所以一起分享给大家吧。

  

  示例我是采用自己开发的EasyCode.Net来设计和生成的,关于EasyCode.Net代码生成器可以参见:

  http://www.cnblogs.com/BudEasyCode/archive/2012/02/27/2370549.html

 

  本示例的相关界面截图及代码如下:

 

 

 

相关获取共享文件夹的代码:

using System;
using System.Diagnostics;
using System.Management;
using System.Collections.Generic;
using System.Text;

namespace BudStudio.NetShare.SFL
{
    public class NetShareHelper
    {
        /// <summary>
        /// WMI方式获取共享文件夹
        /// </summary>
        public static string GetNetShareByWMI()
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            ManagementObjectSearcher searcher = new ManagementObjectSearcher("select  *  from  win32_share");
            string shareFolders = "";
            foreach (ManagementObject share in searcher.Get())
            {
                try
                {
                    shareFolders += share["Name"].ToString();
                    shareFolders += " - ";
                    shareFolders += share["Path"].ToString();
                    shareFolders += "\r\n";
                }
                catch (Exception ex)
                {
                    throw new CustomException(ex.Message, ExceptionType.Warn);
                }

            }

            stopwatch.Stop();
            shareFolders += "总计用时:" + stopwatch.ElapsedMilliseconds.ToString() + "毫秒";
            return shareFolders;
        }

        /// <summary>
        /// CMD方式获取共享文件夹
        /// </summary>
        /// <param name="targetMachine">目标主机IP或名称</param>
        public static string GetNetShareByCMD(string targetMachine)
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();
            string shareFolders = "";
            try
            {
                shareFolders = Cmd("Net View \\\\" + targetMachine);
            }
            catch (Exception ex)
            {
                throw new CustomException(ex.Message, ExceptionType.Warn);
            }

            stopwatch.Stop();
            shareFolders += "总计用时:" + stopwatch.ElapsedMilliseconds.ToString() + "毫秒";
            return shareFolders;
        }

        private static string Cmd(string cmd)
        {
            Process p = new Process();
            p.StartInfo.FileName = "cmd.exe";
            p.StartInfo.UseShellExecute = false;
            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.CreateNoWindow = true;
            p.Start();
            p.StandardInput.AutoFlush = true;

            p.StandardInput.WriteLine(cmd);

            p.StandardInput.WriteLine("exit");
            string strRst = p.StandardOutput.ReadToEnd();
            p.WaitForExit();
            p.Close();
            return strRst;
        }


    }
}

 

本示例中的源代码下载:https://files.cnblogs.com/BudEasyCode/NetShare.rar

posted @ 2012-03-18 23:57  爱英思躺  阅读(4316)  评论(3编辑  收藏  举报