Use a network drive in ASP. NET

Source: Internet
Author: User

We created an image site, where images are placed on machine A, and the background management website is placed on machine B. Since it is management, you must delete the image or something. What should we do in this situation?

1. You can create a WebService on machine.

2. You can also make a remote call on machine.

3. Share the image storage directory on machine A and map machine B to a network drive.

Method 3 is determined.

The procedure is as follows:

0. Suppose machine A (image server), the computer name is jsj-01, IP address is 192.168.0.1;

Machine B (website management in the background)

1. Both machine A and machine B create an account with the same name and password, such as coder/123456.

2. Machine A shares the image directory (IMG) and grants the coder read and write permissions. Pay attention to both sharing and security.

3. Create two classes with the following code)

Using system; using system. collections. generic; using system. LINQ; using system. web; using system. runtime. interopservices; public class wnethelper {[dllimport ("records. DLL ", entrypoint =" wnetaddconnection2 ")] Private Static extern uint wnetaddconnection2 (netresource lpnetresource, string lppassword, string lpusername, uint dwflags); [dllimport (" MIP. DLL ", entrypoint =" wnetcancelconnection2 ")] private stati C 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> /// perform local ing for network sharing /// </Summary> /// <Param name = "us Ername "> access user name (Windows system requires a computer name, such as: comp-1 \ User-1) </param> /// <Param name = "password"> access user password </param> /// <Param name = "remotename"> network sharing path (for example: \ 192.168.0.9 \ share) </param> /// <Param name = "localname"> Local ing drive letter </param> /// <returns> </returns> Public static uint wnetaddconnection (string username, string password, string remotename, string localname) {netresource = new netresource (); netresource. d Wscope = 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;} public class logonimpersonate: idisposable {static Public String defaultdomain {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. ru Ntime. interopservices. dllimport ("kernel32.dll")] extern static bool closehandle (intptr handle); [system. runtime. interopservices. dllimport ("success", setlasterror = true)] extern static bool logonuser (string lpszusername, string lpszdomain, string lpszpassword, int dwlogontype, int dwlogonprovider, ref intptr phtoken); intptr token; system. security. principal. windowsimpersonationcontext Conte XT; Public logonimpersonate (string username, string password) {If (username. indexof ("\") =-1) {Init (username, password, defaultdomain);} else {string [] pair = username. split (New char [] {'\'}, 2); Init (pair [1], password, pair [0]) ;}} public logonimpersonate (string username, string password, string domain) {Init (username, password, domain);} void Init (string username, string password, S Tring domain) {If (logonuser (username, domain, password, logon32_logon_interactive, logon32_provider_default, ref token) {bool error = true; try {context = system. security. principal. windowsidentity. impersonate (token); error = false;} finally {If (error) closehandle (token) ;}} else {int err = system. runtime. interopservices. marshal. getlastwin32error (); intptr tempptr = intptr. zero; string MS G = NULL; formatmessage (0x1300, ref tempptr, err, 0, ref MSG, 255, ref tempptr); throw (new exception (MSG ));}}~ Logonimpersonate () {dispose ();} public void dispose () {If (context! = NULL) {try {context. Undo () ;}finally {closehandle (token); Context = NULL ;}}}}

4. Use the network drive Z in the background management website. The method used is to reference the newly written class.

Protected void btndelete_click (Object sender, eventargs e) {// Delete the image using (logonimpersonate imper = new logonimpersonate ("code", "123456") {uint state = 0; if (! Directory. exists ("W:") {state = wnethelper. wnetaddconnection (@ "jsj-01 \ coder", "123456", @ "\ 192.168.0.1 \ IMG", "Z:") ;}if (! State. equals (0) {Throw new exception ("network drive addition error, error code:" + state. tostring ();} file. delete (image path );}}

Map the shared directory to Z.

First, replace the IIS account and then access the network drive. Note that the using is used only when accessing the network drive, and the default identity is restored when the network drive is used up. Otherwise, other operations will not work.

References

Http://www.cnblogs.com/sqzhuyi/archive/2011/01/15/aspnet-remote.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.