Recently, I made a function for all the specified files in the LAN, so I spent some effort to complete some of the functions.
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data; using system. drawing;
Using system. IO; using system. LINQ;
Using system. text;
Using system. Threading. tasks;
Using system. Windows. forms;
Using system. collections;
Using system. runtime. interopservices;
Using system. diagnostics;
Namespace quicksearchtoolsinnet {
Public partial class form1: FORM {
Public form1 (){
Initializecomponent ();
}
Private void btnsearch_click (Object sender, eventargs e ){
# Region this part is mainly used to test a computer, and the search speed will be slow if there are too many
Ilist <string> lstips = getipsbygroup ();
String IP = @ "\ ckf4989v ";
String selectpath = @ "C :\";
Foreach (VAR item in lstips ){
If (item. tolower (). Contains (IP) {selectpath = IP + @"\";}}
# Endregion
String [] sharelist = netsharelist (selectpath );
Fileinfo [] textfiles = NULL;
Array. foreach (sharelist, share => {
String sharepath = selectpath + share;
VaR dicinfo = new directoryinfo (sharepath); // select the directory information
Textfiles = dicinfo. getfiles ("*. *", searchoption. alldirectories );
});
Stringbuilder sb = new stringbuilder ();
If (textfiles! = NULL ){
Foreach (VAR item in textfiles ){
SB. append (item. fullname + "\ r \ n ");
}}
This. richtextbox1.text = sb. tostring ();
// Directoryinfo dir = new directoryinfo (IP );
// Stringbuilder sb = getfiless (DIR );
// MessageBox. Show (sb. tostring ());
}
# Region
[Structlayout (layoutkind. Sequential)]
Protected struct pai_info_1 {
[Financialas (unmanagedtype. lpwstr)]
Public String shi1_netname;
[Financialas (unmanagedtype. U4)]
Public uint shihei type;
[Financialas (unmanagedtype. lpwstr)]
Public String shi1_remark;
}
[Dllimport ("netapi32.dll", entrypoint = "NetShareEnum")]
Protected static extern int NetShareEnum (
[Financialas (unmanagedtype. lpwstr)] string servername,
[Financialas (unmanagedtype. U4)] uint level, out intptr bufptr,
[Financialas (unmanagedtype. U4)] int prefmaxlen,
[Financialas (unmanagedtype. U4)] Out uint entriesread,
[Financialas (unmanagedtype. U4)] Out uint totalentries,
[Financialas (unmanagedtype. U4)] Out uint resume_handle
);
Private string [] netsharelist (string server ){
Intptr buffer; uint entriesread; uint totalentries; uint resume_handle;
//-1 should be used to get all the shares. The example in msdn is written in this way. If 0 is returned, the result is successful.
If (NetShareEnum (server, 1, out buffer,-1, out entriesread, out totalentries, out resume_handle) = 0 ){
Int32 PTR = buffer. toint32 ();
Arraylist alshare = new arraylist ();
For (INT I = 0; I <entriesread; I ++ ){
Pai_info_1 partition info = (pai_info_1) Marshal. ptrtostructure (New intptr (PTR), typeof (pai_info_1); If (partition info. shi1_type = 0) // disk drive type {
Alshare. Add (Response info. shiw.netname );
}
PTR + = marshal. sizeof (partition info); // a bit similar to C code
}
String [] Share = new string [alshare. Count];
For (INT I = 0; I <alshare. Count; I ++) {share [I] = alshare [I]. tostring () ;}return share ;}
Else return NULL;
}
# Endregion
# Region obtain the names of all computers in the workgroup
Private ilist <string> getipsbygroup (){
Processstartinfo info = new processstartinfo ("cmd.exe ");
Info. Arguments = "/K net View"; // the input and output streams can be captured only when the process is executed without the shell.
Info. useshellexecute = false; // redirect the input stream
// Info. redirectstandardinput = true;
Info. redirectstandardoutput = true; // redirect the output stream
PROCESS p = process. Start (Info );
Streamreader output = P. standardoutput; // writer used to obtain the output stream
Ilist <string> lstips = new list <string> ();
While (! Output. endofstream) {string line = output. Readline (); If (line. startswith ("\\\\") lstips. Add (line);} return lstips ;}
# Endregion
# Region recursively retrieve all files in a directory (including files in subdirectories) ---------------- discarded
/// <Summary> /// recursively retrieve all files in a directory (including files in subdirectories) -------------- obsolete /// </Summary>
/// <Param name = "dir"> outermost folder path </param>
/// <Returns> </returns>
Private stringbuilder getfiless (directoryinfo DIR ){
Fileinfo [] files = dir. getfiles ();
Stringbuilder sb = new stringbuilder ();
Foreach (VAR item in files ){
SB. append (item. fullname + "\ r \ n ");
}
If (dir. getdirectories (). length> 0 ){
Directoryinfo [] dirs = dir. getdirectories ();
Foreach (VAR item in dirs ){
SB. append (getfiless (item) + "\ r \ n ");
}}
Return Sb ;}
# Endregion
}}