Home > Net >  In c # how to determine the machine have Shared a folder
In c # how to determine the machine have Shared a folder

Time:01-09

I , just need to decide the machine a folder to see if there is Shared ,
If it is judged through the Shared folder, need to consider the Shared folder name and folder name is not the same ,

If the operating system to consider, there will be the following:
Windows XP
Windows 2003
Windows 2008
Windows 7
Windows 10

CodePudding user response:

I'm in the following address
Query to the following method in https://bbs.csdn.net/topics/30232186
Read registry
98/me: HKEY_LOCAL_MACHINE \ \ Software \ \ Microsoft \ \ Windows \ \ CurrentVersion \ \ Network \ \ LanMan \ \

2 k/XP: HKEY_LOCAL_MACHINE \ SYSTEM \ ControlSet001 \ Services \ lanmanserver \ Shares

This method, possible: "the Shared folder name" with "folder name" is not the same,

CodePudding user response:

Directly run CMD command invocation net share good
 
///& lt; Summary>
///DOS command, return the output of the DOS command
///& lt;/summary>
///& lt; Param name="dosCommand & gt;" DOS command & lt;/param>
///& lt; Param name="milliseconds" & gt; Waiting for the command execution time (unit: ms), if set to 0, the infinite wait & lt;/param>
///& lt; Returns> Returns the output, if an exception occurs, returns an empty string & lt;/returns>
Public static string ExecuteDos (string dosCommand, int milliseconds)
{
String output="";//output string
If (dosCommand!=null & amp; & DosCommand!="")
{
Process Process=new Process ();//create the process object
ProcessStartInfo startInfo=new ProcessStartInfo ();
StartInfo. FileName="CMD. Exe";//set to execute commands
StartInfo. The Arguments="/" C + dosCommand;//set the parameters, the execution of the command "/C" said exit immediately after
StartInfo. UseShellExecute=false;//do not use the system shell start
StartInfo. RedirectStandardInput=false;//not redirect input
StartInfo. RedirectStandardOutput=true;//redirecting output
StartInfo. CreateNoWindow=true;//do not create window
Process. StartInfo=StartInfo;
Try
{
If (process. The Start ())//process
{
If (milliseconds==0)
Process. WaitForExit ();//here infinite waiting for the end of the process
The else
Process. WaitForExit (milliseconds);//wait for the process to end here, waiting time for the specified milliseconds
The output=process. StandardOutput. ReadToEnd ();//read process output
}
}
Catch
{
}
The finally
{
If (the process!=null)
Process. The Close ();
}
}
Return the output;
}
The string result=ExecuteDos (" net share ", 3000);

CodePudding user response:

 public static List GetSharedFolders () 
{

List SharedFolders=new List (a);

//the Object to query the WMI Win32_Share API for Shared files...

ManagementObjectSearcher searcher=new ManagementObjectSearcher (" select * from win32_share ");

ManagementBaseObject outParams;

Win32_Share ManagementClass MC=new ManagementClass (" ");//for local shares

Foreach (ManagementObject share in a searcher. The Get ()) {

String type=share [r]. "the type" the ToString ();

If (type=="0")//0=DiskDrive (1=the Print Queue, 2=Device, 3=IPH)
{
String name=share [r]. "the name" the ToString ();//getting share name

String path=share [r]. "path" ToString ();//getting share path

String caption=share [r]. "caption" ToString ();//getting share the description

SharedFolders. Add (path);
}

}

Return sharedFolders;

}
  •  Tags:  
  • C#
  • Related