About accessing the C # client on the web, causing the C # client to fail to access the network map disk problem
About the problem is this, before doing a project, need to use the Java page button, call the C # client on the server (EXE file), C # code has access to the network map disk path assumed to be x disk, so on the server directly start EXE file is no problem If the EXE file is fetched through the page button, the EXE will not be able to access the map disk path. To solve this problem you need to add C # code, that is, the new implementation of network drive disk sharing code, but also found the relevant code on the Internet, but the use of detail is not detailed enough, now sorted out to share out, Although the use of the situation is not much, but encountered this problem is still relatively tangled.
1. New C # console project
2, New LogonImpersonate.cs class
Using System; public class Logonimpersonate:idisposable { static public string DefaultDomain { &NBS P Get : return "."
} } const int logon32_logon_interactive = 2;
const int logon32_provider_default = 0; [System.Runtime.InteropServices.DllImport ("Kernel32.dll")] extern static int FormatMessage
(int flag, ref INTPTR source, int msgid, int langid, ref string buf, int size, ref INTPTR args); [System.Runtime.InteropServices.DllImport ("Kernel32.dll")] extern static bool CloseHandle (
INTPTR handle); [System.Runtime.InteropServices.DllImport ("Advapi32.dll", SetLastError = True)] extern static bool LogonUser ( String lpszUserName, string lpszdomain, string Lpszpassword, int dwLogonType, int dwLogonProvider, ref INTPTR Phtoken & nbsp
);
INTPTR token;
System.Security.Principal.WindowsImpersonationContext context; Public logonimpersonate (string Username, string password) { -if (US Ername. IndexOf ("//") = = 1) { Init (username, password, Defa
Ultdomain); } else { & nbsp string[] pair = username.
Split (new char[] {'/'}, 2);
Init (pair[1], password, pair[0]); } { public logonimpersonate (string Username, string password, stri ng domain) { Init (username, password, domain); } void Init (string username, string password, string domain) { IF (LogonUser (username, domain, password, logon32_logon_interactive, Logon32_provider_default, ref token)) & nbsp
{ BOOL error = TRUE; Try { context = System.secu Rity.
Principal.WindowsIdentity.Impersonate (token);
error = false; finally ( if (error) &NBS P
CloseHandle (token); } { &NBSp Else { int err = System.Runtime.InteropServic Es.
Marshal.GetLastWin32Error ();
INTPTR tempptr = IntPtr.Zero;
string msg = null; FormatMessage (0x1300, ref TEMPPTR, err, 0, ref MSG, 255, ref   ;
TEMPPTR);
throw (new Exception (msg)); } { ~logonimpersonate () { &NBSP ;
Dispose (); } public void Dispose () { != null   ; { try { &N Bsp context.
Undo (); } &nbsP finally {
CloseHandle (token);
context = null;
{ } }
3, New WNetHelper.cs class
Using System.Runtime.InteropServices;
Using System.IO;
Using System;
public class Wnethelper
{
[DllImport ("Mpr.dll", EntryPoint = "WNetAddConnection2")]
private static extern uint WNetAddConnection2 (Netresource lpnetresource, String Lppassword, String lpusername, uint DWFLA GS);
[DllImport ("Mpr.dll", EntryPoint = "WNetCancelConnection2")]
private static extern uint WNetCancelConnection2 (string lpname, uint dwflags, bool fforce);
[StructLayout (LayoutKind.Sequential)]
public class Netresource
{
public int dwscope;
public int dwtype;
public int dwdisplaytype;
public int dwusage;
public string lpLocalName;
public string Lpremotename;
public string lpcomment;
public string Lpprovider;
}
<summary>
Make local mappings for network shares
</summary>
<param name= "username" > Access username (Windows system needs to add computer name, such as: comp-1/user-1) </param>
<param name= "Password" > Access user password </param>
<param name= "remotename" > Network share path (such as://192.168.0.9/share) </param>
<param name= "LocalName" > Local map Letter </param>
<returns></returns>
public static UINT Wnetaddconnection (string username, string password, string remotename, String localname)
{
Netresource Netresource = new Netresource ();
Netresource.dwscope = 2;
Netresource.dwtype = 1;
Netresource.dwdisplaytype = 3;
Netresource.dwusage = 1;
Netresource.lplocalname = LocalName;
Netresource.lpremotename = Remotename.trimend ('/');
UINT result = WNetAddConnection2 (netresource, password, username, 0);
return result;
}
public static UINT Wnetcancelconnection (string name, UINT flags, BOOL force)
{
UINT nret = WNetCancelConnection2 (name, flags, force);
return nret;
}
}
4, the implementation of drive mapping, pay attention not to judge useless, but must be added, otherwise access network drive disk.
static void Main (string[] args
{
//=========== ' Loads the network map disk code, which requires the following code when calling the secondary application over the Web, otherwise the access path fails ===================
uint state = 0;
if (! Directory.Exists ("Z:"))
{
Wnethelper.wnetaddconnection (@ "Computer name \ Login Account", "Login password", @ "map path", @ "map letter name");
State = Wnethelper.wnetaddconnection (@ "Cqqxyjs\yjsnyygyjs", "Yjsnyygyjs27", @ "\\192.168.1.110\micaps", @ "Z:");
}
if (state. Equals (0))
{
}
Else
{
Console.WriteLine ("Add Network drive Error, error number:" + state. ToString ());
}
}