Shared catalog

xiaoxiao2021-03-05  26

Public String Connectlan ()

{

Process P;

p = new process ();

p.Startinfo.fileName = "cmd.exe";

/ / Here is a key point, no shell boot / redirect input / redirection output / not display window

P.Startinfo.uShellexecute = false;

P.Startinfo.redirectStandardInput = true;

P.Startinfo.RedirectStandardOrdoutput = True;

P.StartInfo.createnowindow = true;

p.Start ();

P.Standardinput.writeline ("NET USE NT02 Password / User: User Name"); // Enter your COMMAND to cmd.exe

P.standardinput.writeline ("exit");

p.WaitForexit ();

String s = p.standardoutput.readtoend (); // Get the output of cmd.exe

p.Close ();

Return S;

}

I finally know how to get a shared directory.

[Structlayout (layoutkind.sequential)]

Protected struct share_info_1

{

[Marshalas (UnmanagedType.lpwstr)] public string shi1_netname;

[Marshalas (unmanagedType.u4)] public uint shi1_type;

[Marshalas (unmanagedType.lpwstr)] public string shi1_remark;

}

// 98 No this function?

[DLLIMPORT ("Netapi32.dll", entrypoint = "netshareenum")]

protected static extern int netshareenum

[Marshalas (UnmanagedType.lpwstr)] String ServerName,

[Marshalas (unmanagedtype.u4)] uint level,

Out INTPTR BUFPTR,

[Marshalas (unmanagedType.u4)] int Prefmaxlen,

[Marshalas (unmanagedtype.u4)] OUT uint EntriesRead,

[Marshalas (UnmanagedType.u4)] Out uint Totalentries,

[Marshalas (unmanagedType.u4)] OUT uint resume_handle

);

Private string [] netshareenum (String Server)

{

INTPTR BUFFER;

Uint EntriesRead;

Uint Totalentries;

uint resume_handle;

// - 1 It should be to get all Share, the example inside MSDN is written, return 0 means success

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

{

Share_info_1 shareinfo = (Share_info_1) Marshal.PTRTOStructure (New INTPTR (PTR), TypeOf (Share_info_1)); if (ShareInfo.shi1_Type == 0) // Disk Drive Type

{

Alshare.Add (ShareInfo.shi1_netName);

}

Ptr = marshal.sizeof (ShareInfo); // a little similar C code

}

String [] Share = new string [alshare.count];

For (int i = 0; i

{

Share [i] = alshare [i] .tostring ();

}

Return Share;

}

Else

Return NULL;

}

转载请注明原文地址:https://www.9cbs.com/read-35238.html

New Post(0)