[Share] Use CMD/WMI to get shared folders -- and performance comparison between the two methods

Source: Internet
Author: User

A friend wrote an article on the Internet about how to use C # To obtain the shared folder of a local computer.Article, Read it.CodeThe WMI method is used, that is, the classes and methods in system. Management are called to obtain the shared folder of the computer. I remember that I had a project that needed to obtain hardware information a few years ago. At that time, I used the WMI method, which left me the impression that WMI was quite slow. Therefore, I wrote a test and found that WMI is not slow in obtaining shared folders. It may be slow in obtaining some specific hardware information.

 

The test example I wrote contains two tests: CMD and WMI. The test result is that CMD is slower than WMI, after all, it takes time to start a thread. In this example, wmi ping.

For example, I developed it myself.EASYCODE. netFor the EASYCODE. Net code generator, see:

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

 

The interface and code for this example are as follows:

 

 

 

Related code for getting Shared Folders:

Using system; using system. diagnostics; using system. management; using system. collections. generic; using system. text; namespace budstudio. netshare. sfL {public class netw.helper {/// <summary> // use the WMI method to obtain the shared folder /// </Summary> Public static string getnetsharebywmi () {stopwatch = new stopwatch (); stopwatch. start (); managementobjectsearcher searcher = new managementobjectsearcher ("select * From win32_share"); string comment folders = ""; foreach (managementobject share in searcher. get () {try {folder folders + = Share ["name"]. tostring (); returns folders + = "-"; returns folders + = Share ["path"]. tostring (); writable folders + = "\ r \ n";} catch (exception ex) {Throw new customexception (ex. message, exceptiontype. warn) ;}} stopwatch. stop (); When folders + = "Total time:" + stopwatch. elapsedmilliseconds. tostring () + "millisecond"; return response folders ;} /// <summary> /// obtain the shared folder in cmd mode /// </Summary> /// <Param name = "targetmachine"> Target Host IP address or name </Param> Public static string getnetsharebycmd (string targetmachine) {stopwatch = new stopwatch (); stopwatch. start (); string character folders = ""; try {character folders = cmd ("net view \" + targetmachine);} catch (exception ex) {Throw new customexception (ex. message, exceptiontype. warn);} stopwatch. stop (); When folders + = "Total time:" + stopwatch. elapsedmilliseconds. tostring () + "millisecond"; return response folders;} 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 ;}}}

 

In this exampleSource codeDownload: http://files.cnblogs.com/BudEasyCode/NetShare.rar

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.