tag:blogger.com,1999:blog-10979178.post114294839426884567..comments2010-07-28T11:25:54.280+05:30Comments on Corner House: Accessing Shared Folders in ASP.NETAgnel CJ Kurianhttp://www.blogger.com/profile/02108024180511317456noreply@blogger.comBlogger25125tag:blogger.com,1999:blog-10979178.post-43940791269848981682010-07-06T13:17:16.587+05:302010-07-06T13:17:16.587+05:30Hi Adeel,
I have found the solution for my proble...Hi Adeel,<br /><br />I have found the solution for my problem. I didn't notice that I need to specify the file name instead of just specify the path name. Example: string strPhysicalPath = @"\\SharedFolder\File\testing.doc";<br /><br />Anyway, thanks for the reply.<br /><br />Regards,<br />Howyi十二月https://www.blogger.com/profile/00339842408731688273noreply@blogger.comtag:blogger.com,1999:blog-10979178.post-61515842321160934962010-07-01T14:31:49.487+05:302010-07-01T14:31:49.487+05:30you need to Impersonate to access a shared folder ...you need to Impersonate to access a shared folder from IIS.<br /><br />Please follow the example above.Adeelnoreply@blogger.comtag:blogger.com,1999:blog-10979178.post-40798270980792353192010-07-01T06:56:03.429+05:302010-07-01T06:56:03.429+05:30Hi,
I'm new in asp.net, C#. Your code is quit...Hi,<br /><br />I'm new in asp.net, C#. Your code is quite complicated for me. I don't really get it. Can you explain further? Currently I'm doing a web application, and I want to access to shared folder. Can you please help take a look on my codes below?<br /><br />string strPhysicalPath = @"\\SharedFolder\File\";<br />FileInfo objFileInfo = new FileInfo(strPhysicalPath);<br />Response.Clear();<br />Response.CacheControl = "private";<br />Response.Expires = -1;<br />Response.ContentType = "application/octet"; Response.AddHeader("Content-Disposition", "attachment; filename=testing.bmp");<br /><br />FileStream fs = new FileStream(strPhysicalPath, FileMode.Open, FileAccess.Read);<br />byte[] buffer = new Byte[objFileInfo.Length];<br />fs.Read(buffer, 0, (int)objFileInfo.Length); Response.BinaryWrite(buffer);<br /><br />fs.Close();<br />Response.End();<br /><br />Is there something wrong with my code?<br /><br />Regards,<br />Howyi十二月https://www.blogger.com/profile/00339842408731688273noreply@blogger.comtag:blogger.com,1999:blog-10979178.post-33833070487469521822009-11-10T16:57:24.299+05:302009-11-10T16:57:24.299+05:30The code below is working fine, I was just doing a...The code below is working fine, I was just doing a mistake passing Remote machine name instate of local_machine name, tha solve the problem.<br /><br />let me know if this not works.<br />***********************************<br />using System;<br />using System.Data;<br />using System.Configuration;<br />using System.Linq;<br />using System.Web;<br />using System.Web.Security;<br />using System.Web.UI;<br />using System.Web.UI.HtmlControls;<br />using System.Web.UI.WebControls;<br />using System.Web.UI.WebControls.WebParts;<br />using System.Xml.Linq;<br />using System.Security.Principal;<br />using System.Runtime.InteropServices;<br /><br />/// summary<br />/// Summary description for ImpersonateUser<br /><br />public class ImpersonateUser<br />{<br /> // Declare signatures for Win32 LogonUser and CloseHandle APIs<br /> [DllImport("advapi32.dll", SetLastError = true)]<br /> static extern bool LogonUser(<br /> string principal,<br /> string authority,<br /> string password,<br /> LogonSessionType logonType,<br /> LogonProvider logonProvider,<br /> out IntPtr token);<br /> [DllImport("kernel32.dll", SetLastError = true)]<br /> static extern bool CloseHandle(IntPtr handle);<br /> enum LogonSessionType : uint<br /> {<br /> Interactive = 2,<br /> Network,<br /> Batch,<br /> Service,<br /> NetworkCleartext = 8,<br /> NewCredentials<br /> }<br /> enum LogonProvider : uint<br /> {<br /> Default = 0, // default for platform (use this!)<br /> WinNT35, // sends smoke signals to authority<br /> WinNT40, // uses NTLM<br /> WinNT50 // negotiates Kerb or NTLM<br /> }<br /><br /> <br /><br /> static WindowsImpersonationContext wic;<br /><br /> public ImpersonateUser() { }<br /> private static WindowsImpersonationContext impersonatedUser = null;<br /> private static IntPtr token = new IntPtr(0);<br /><br /><br /> public void Impersonate(string username)<br /> {<br /> //IntPtr token = IntPtr.Zero;<br /> token = IntPtr.Zero;<br /> <br /><br /> try<br /> {<br /> // Create a token for DomainName\Bob<br /> // Note: Credentials should be encrypted in configuration file<br /> bool result = LogonUser(username, "LOCAL_SERVER_NAME",<br /> "LOGIN_PASSWORD",<br /> LogonSessionType.NetworkCleartext,<br /> LogonProvider.Default,<br /> out token);<br /> if (result)<br /> {<br /> WindowsIdentity id = new WindowsIdentity(token);<br /><br /> // Begin impersonation<br /> impersonatedUser = id.Impersonate();<br /> // Log the new identity <br /> <br /> }<br /> else<br /> {<br /> Email.sendError("adeel@xxx.com", "Error (I/O) impersonate failed: " +username + " ", Marshal.GetLastWin32Error().ToString()); <br /> }<br /> }<br /> catch (Exception ex)<br /> {<br /> Email.sendError("adeel@xxx.com", "Error (I/O) impersonate: "+username + " ", ex.Message); <br /> // Prevent any exceptions that occur while the thread is <br /> // impersonating from propagating<br /> }<br /> }<br /><br /> // Stops impersonation<br /> public void Undo()<br /> {<br /> if (impersonatedUser != null)<br /> {<br /> impersonatedUser.Undo();<br /> // Free the tokens.<br /> if (token != IntPtr.Zero)<br /> CloseHandle(token);<br /><br /> // Email.sendError("adeel@xxx.com", "impersonate Undo: ", "impersonate Undo: ");<br /> } <br /> }<br /><br /><br /><br />}adeelhttps://www.blogger.com/profile/16110213303502478905noreply@blogger.comtag:blogger.com,1999:blog-10979178.post-41752699896930727282009-11-09T17:31:13.030+05:302009-11-09T17:31:13.030+05:30Hi Adeel, I'm facing the same problem as yours...Hi Adeel, I'm facing the same problem as yours, did you manage to solve it please ?<br /><br />ThanxUnknownhttps://www.blogger.com/profile/09386491865894188274noreply@blogger.comtag:blogger.com,1999:blog-10979178.post-38291279717145145122009-06-25T21:56:25.757+05:302009-06-25T21:56:25.757+05:30error: Access to the path '\\192.168.100.100\s...error: Access to the path '\\192.168.100.100\shared\Adeel.txt' is denied.adeelhttps://www.blogger.com/profile/16110213303502478905noreply@blogger.comtag:blogger.com,1999:blog-10979178.post-66917697198708758512009-06-25T21:32:54.801+05:302009-06-25T21:32:54.801+05:30I'd check to see if the access denied error is...I'd check to see if the access denied error is happening on the source or the destination. Have a closer look at the exception and let me know.Agnel CJ Kurianhttps://www.blogger.com/profile/02108024180511317456noreply@blogger.comtag:blogger.com,1999:blog-10979178.post-16804873868391778832009-06-25T20:41:25.757+05:302009-06-25T20:41:25.757+05:30Hi Kurian,
Thank for your unstant reply.
The fol...Hi Kurian,<br /><br />Thank for your unstant reply.<br /><br />The folder on the remote machine as shared and have full control a shared user everyone, which is on both machine with same name and passowrd and Administrator rights.<br /><br />Remote machine running Windows vista business.<br /><br />please see source code below.<br /><br />public class ShareThis<br />{<br /><br /> private const string share=@"\\192.168.100.100\shared";<br /> private const string RemoteDir = @"\\192.168.100.100\shared";<br /> private const string LocalDir = @"c:\local";<br /> private const string username = "username";<br /> private const string password = "password";<br /><br /><br /> //used in calling WNetAddConnection2 <br /> [StructLayout (LayoutKind.Sequential)] <br /> public struct NETRESOURCE<br /> {<br /> public int dwScope;<br /> public int dwType;<br /> public int dwDisplayType;<br /> public int dwUsage;<br /> [MarshalAs(UnmanagedType.LPStr)]<br /> public string lpLocalName;<br /> [MarshalAs(UnmanagedType.LPStr)]<br /> public string lpRemoteName;<br /> [MarshalAs(UnmanagedType.LPStr)]<br /> public string lpComment;<br /> [MarshalAs(UnmanagedType.LPStr)]<br /> public string lpProvider;<br /> }<br /> //WIN32API - WNetAddConnection2 <br /> [DllImport("mpr.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]<br /> private static extern int WNetAddConnection2A([MarshalAs(UnmanagedType.LPArray)] NETRESOURCE[] lpNetResource,<br /> [MarshalAs(UnmanagedType.LPStr)] string lpPassword,<br /> [MarshalAs(UnmanagedType.LPStr)] string lpUserName,<br /> int dwFlags);<br /> //WIN32API - WNetCancelConnection2 <br /> [DllImport("mpr.dll", CharSet = System.Runtime.InteropServices.CharSet.Auto)]<br /> private static extern int WNetCancelConnection2A([MarshalAs(UnmanagedType.LPStr)] string lpName, int dwFlags, int fForce);<br /> <br /> //public static void CopyFile(string share, string username, string password, string dirFrom, string dirTo, string filename)<br /> public static void CopyFileToPAF(string filename)<br /> {<br /> NETRESOURCE[] nr = new NETRESOURCE[1];<br /> nr[0].lpRemoteName = share;<br /> nr[0].lpLocalName = "";<br /> //mLocalName; <br /> nr[0].dwType = 1;<br /> //disk <br /> nr[0].dwDisplayType = 0;<br /> nr[0].dwScope = 0;<br /> nr[0].dwUsage = 0;<br /> nr[0].lpComment = "";<br /> nr[0].lpProvider = "";<br /> WNetAddConnection2A(nr, password, username, 0);<br /> File.Copy(LocalDir + "\\" + filename, RemoteDir + "\\" + filename, true); <br /> WNetCancelConnection2A(share, 0, -1); <br /> }<br /><br /> public static void CopyFileToLocal(string filename)<br /> {<br /> NETRESOURCE[] nr = new NETRESOURCE[1];<br /> nr[0].lpRemoteName = share;<br /> nr[0].lpLocalName = "";<br /> //mLocalName; <br /> nr[0].dwType = 1;<br /> //disk <br /> nr[0].dwDisplayType = 0;<br /> nr[0].dwScope = 0;<br /> nr[0].dwUsage = 0;<br /> nr[0].lpComment = "";<br /> nr[0].lpProvider = "";<br /> WNetAddConnection2A(nr, password, username, 0);<br /> File.Copy(RemoteDir + "\\" + filename, LocalDir + "\\" + filename,true);<br /> WNetCancelConnection2A(share, 0, -1);<br /> }adeelhttps://www.blogger.com/profile/16110213303502478905noreply@blogger.comtag:blogger.com,1999:blog-10979178.post-19188476643320197802009-06-25T20:20:30.256+05:302009-06-25T20:20:30.256+05:30@adeel
Explain this: "When I tried same code ...@adeel<br />Explain this: "When I tried same code on Windows Server 2008 with IIS 7 gives error Access denied to the path, but I can share the path on server manualy."Agnel CJ Kurianhttps://www.blogger.com/profile/02108024180511317456noreply@blogger.comtag:blogger.com,1999:blog-10979178.post-88148100861574002412009-06-25T14:05:43.651+05:302009-06-25T14:05:43.651+05:30The folder is shared.The folder is shared.adeelhttps://www.blogger.com/profile/16110213303502478905noreply@blogger.comtag:blogger.com,1999:blog-10979178.post-61634886083247880832009-06-25T11:03:20.881+05:302009-06-25T11:03:20.881+05:30The folder must be shared before calling the funct...The folder must be shared <i>before</i> calling the function. The function only copies files; it does <i>not</i> share any folders.Agnel CJ Kurianhttps://www.blogger.com/profile/02108024180511317456noreply@blogger.comtag:blogger.com,1999:blog-10979178.post-48339985010804061552009-06-24T20:31:15.883+05:302009-06-24T20:31:15.883+05:30When I tried your code on my local machine in visu...When I tried your code on my local machine in visual Studio 2008 with a shared path CopyFile works fine.<br /><br />When I tried same code on Windows Server 2008 with IIS 7 gives error Access denied to the path, but I can share the path on server manualy.<br /><br />Please advise me what I am doing wrong.adeelhttps://www.blogger.com/profile/16110213303502478905noreply@blogger.comtag:blogger.com,1999:blog-10979178.post-90394705404088819432009-06-10T13:42:42.723+05:302009-06-10T13:42:42.723+05:30@amirLURVEnabilah
You should be able to use ShellE...@amirLURVEnabilah<br />You should be able to use ShellExecute to attempt 'opening' the file. Downloading? That is what my sample code does.<br /><br />@Siva<br />.NET has File,Path and Directory classes. Try some of the static methods.<br /><br />@Sabarishbabu<br />I think you will have to make another call to WNetAddConnection2A with the login and password to the second machine.<br /><br />Sorry for the late reply.Agnel CJ Kurianhttps://www.blogger.com/profile/02108024180511317456noreply@blogger.comtag:blogger.com,1999:blog-10979178.post-73258915644826078482009-05-28T15:29:24.459+05:302009-05-28T15:29:24.459+05:30Hi,
1.I am using your example to create a Director...Hi,<br />1.I am using your example to create a Directory in our shared network (FileServer is part of our network).<br />2.I followed your code to copy a file.<br />3. It works successfully when I gave the source and destination machines are same. Able to copy a file.<br />But, I am getting "Exception - Logon failure: unknown user name or bad password." error when I am gave the different machine name <br />Like<br />CopyFile(@"\\sfg22-135561864", "SFG22", "welcome", @"E:\shared",<br /> @"\\192.168.1.65\d\Files", "123.txt");. <br /><br />Any ideas/clues<br /><br />Thanks a lot for your quick response<br />Sabarish<br />sabarishbabu@gmail.conSabarishbabuhttps://www.blogger.com/profile/02223180256373882802noreply@blogger.comtag:blogger.com,1999:blog-10979178.post-41331735248505217332009-05-28T15:28:07.278+05:302009-05-28T15:28:07.278+05:30Hi,
1.I am using your example to create a Director...Hi,<br />1.I am using your example to create a Directory in our shared network (FileServer is part of our network).<br />2.I followed your code to copy a file.<br />3. It works successfully when I gave the source and destination machines are same. Able to copy a file.<br />But, I am getting "Exception - Logon failure: unknown user name or bad password." error when I am gave the different machine name <br />Like<br />CopyFile(@"\\sfg22-135561864", "SFG22", "welcome", @"E:\shared",<br /> @"\\192.168.1.65\d\Files", "123.txt");. <br /><br />Any ideas/clues<br /><br />Thanks a lot for your quick response<br />Sabarish<br />sabarishbabu@gmail.conSabarishbabuhttps://www.blogger.com/profile/02223180256373882802noreply@blogger.comtag:blogger.com,1999:blog-10979178.post-72725322731555833662009-05-28T14:38:47.302+05:302009-05-28T14:38:47.302+05:30Is it possible to retrive the contents of the shar...Is it possible to retrive the contents of the shared folders i.e. name of the files using simillar method . any inputs?K Siva Karthikeyanhttps://www.blogger.com/profile/05342435755106347987noreply@blogger.comtag:blogger.com,1999:blog-10979178.post-19697835987301874662009-05-27T09:05:33.667+05:302009-05-27T09:05:33.667+05:30hi..this code really help me..
do you know how to ...hi..this code really help me..<br />do you know how to open or download files from the shared folder?amirLURVEnabilahhttps://www.blogger.com/profile/07865673397262391907noreply@blogger.comtag:blogger.com,1999:blog-10979178.post-44351335228134837502008-06-14T13:04:00.000+05:302008-06-14T13:04:00.000+05:30@kjsteuer I am not sure of that. You can read more...@kjsteuer <BR/>I am not sure of that. You can read more about the SMB protocol to get that answer (SMB is how we access shared folders on Windows).Agnel CJ Kurianhttps://www.blogger.com/profile/02108024180511317456noreply@blogger.comtag:blogger.com,1999:blog-10979178.post-83194147574958960682008-06-02T19:38:00.000+05:302008-06-02T19:38:00.000+05:30Do you know if the password encrypted over the net...Do you know if the password encrypted over the network?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-10979178.post-67001902517684823232008-04-25T03:02:00.000+05:302008-04-25T03:02:00.000+05:30YOU SO TOTALLY ROCK! This works wonderfully. I t...YOU SO TOTALLY ROCK! This works wonderfully. I took your code and modified it to what i needed.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-10979178.post-32144015431888965412007-05-05T10:25:00.000+05:302007-05-05T10:25:00.000+05:30Swap the 4th and 5th arguments to CopyFile()Swap the 4th and 5th arguments to CopyFile()Agnel CJ Kurianhttps://www.blogger.com/profile/02108024180511317456noreply@blogger.comtag:blogger.com,1999:blog-10979178.post-18313969027023886322007-05-04T18:59:00.000+05:302007-05-04T18:59:00.000+05:30Hey how Do I acieve Reverse ?? i.e from n/w to cli...Hey how Do I acieve Reverse ?? <BR/><BR/>i.e from n/w to client machine ??Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-10979178.post-1146842873640580852006-05-05T20:57:00.000+05:302006-05-05T20:57:00.000+05:30VB version should be easy. ;)VB version should be easy. ;)Agnel CJ Kurianhttps://www.blogger.com/profile/02108024180511317456noreply@blogger.comtag:blogger.com,1999:blog-10979178.post-1146839595060975252006-05-05T20:03:00.000+05:302006-05-05T20:03:00.000+05:30That looks great...But do you have a VB version?That looks great...<BR/><BR/>But do you have a VB version?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-10979178.post-1146129947793566402006-04-27T14:55:00.000+05:302006-04-27T14:55:00.000+05:30Hi, Your code for moving file to network share rea...Hi,<BR/> Your code for moving file to network share really helped me. <BR/><BR/><BR/>Thankyou,<BR/>Mahesh.<BR/>Developer.Anonymousnoreply@blogger.com