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