Click here to Skip to main content
15,885,782 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to create folder on client machine from my asp.net web application and then transfer multiple files into this folder from server to client machine using C#. I have used C# windows impersonation using which i can create folder and also transfer files until and unless file and printer sharing for microsoft networks(network protocol) is checked, But when i uncheck this network protocol on client machine then my code give me error "network path not found".Please tell me if you people have any other option to transfer folder on multiple client machine using asp.net web application. I am stuck in this problem more than a week.

please find my code below:-

public void CreateDir(string DriverPath)
        {
            IntPtr token = IntPtr.Zero;

            WindowsImpersonationContext impersonatedUser = null;
            Hashtable Fod_Files = new Hashtable();

            try
            {
                // Create a token for DomainName\UserName

                //Call LogonUser  function to obtain an access token 
                // for the specified user

                client_Uname = "a1093557";
                //  client_IPAddress = "india.airtel.itm";
                client_pwd = "sairam@9";
                DomainName = "india.airtel.itm";


                bool result = LogonUser(client_Uname, DomainName,
                                       client_pwd,
                                       LogonSessionType.NewCredentials,
                                       LogonProvider.Default,
                                       out token);

                if (result)
                {
                    //create a new window identity from the window access token

                    WindowsIdentity id = new WindowsIdentity(token);

                    // Begin impersonation

                    impersonatedUser = id.Impersonate();

                    Drv_FolderPath = @"C:\PrntDriver1";
                    DriverPath = @"\\10.14.65.85\C$\hp";

                    if (Directory.Exists(Drv_FolderPath))
                    {
                        //DeleteDir();
                    }

                    // Drv_FolderPath = @"\\" + client_IPAddress + @"\C$\PrntDriver";

                    System.IO.Directory.CreateDirectory(Drv_FolderPath);
                    Fod_Files = Chk_Files(DriverPath);
                    string[] FileName = null;
                    string[] Drv_Fold = null;
                    Drv_Fold = DriverPath.Split('\\');
                    int i = Drv_Fold.Length;
                    //loop on  hashtable which contains all the file with their path 
                    foreach (string f in Fod_Files.Values)
                    {
                        FileName = f.Split('\\');

                        string Dest_Path = Drv_FolderPath + @"\" + FileName[i];

                        if (!File.Exists(Dest_Path))
                        {
                            //it will copy files on client machine
                            System.IO.File.Copy(f, Dest_Path);
                        }
                    }
                    //   utl.putLogData(DateTime.Now.ToString() + "  Driver setup copied successfully ");
                }

                else
                {
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                // Stop impersonation and revert to the process identity
                if (impersonatedUser != null)
                    impersonatedUser.Undo();
                //// Free the token
                if (token != IntPtr.Zero)
                    CloseHandle(token);
            }
        }
Posted
Updated 14-Feb-12 1:44am
v3
Comments
Varun Sareen 14-Feb-12 7:44am    
edited for: added pre tag

1 solution

This is a security issue. A server app needs permissions to create anything on the client side so you'll have to go through the proper route or create a client side application that can do the work for you.
 
Share this answer
 
Comments
Member 7979150 15-Feb-12 1:01am    
I cannot create client side application.I have to achieve this target using web application.Could you please tell me how to give permissions to server app so that i can create folder on client side. My above solution is working fine if file and printer sharing for microsoft networks checkbox is checked. But when client machine is on domain then due to policies this network protocol is unchecked due to which my above mentioned code fail and give me error "network path not found". If anybody have any idea how to check/uncheck file and printer sharing for microsoft networks on client machine using web application then please help me. I also try to enable file and printer sharing for microsoft network using vb script but that's also not working. Do you any solution using which i can transfer folder on different clients machine using asp.net web application. Please help . It's urgent. I am stuck in this problem more than a week but still not find any solution.
ZurdoDev 15-Feb-12 8:03am    
Like I said, it is a security issue. You are not supposed to be able to create things on the client side from the server side. If you could, anyone could easily create a website that could hack and destroy every computer that browses to it. You have to go through the network protocols to do what you want.
Member 7979150 16-Feb-12 7:23am    
Thank you so much for reply.
I have permissions to create folder on client machine because i have domain id who have administrator rights on domain. using that id i can do anything on those client machine which are on domain. That's why above mentioned code run successfully if i manually checked the protocol "file and printer sharing for microsoft networks"(network protocol). But when i uncheck this checkbox then it will give me error "network path not found" because i cannot even access that machine by typing its ip address in run command.you tell me i need to go through network protocols could you please tell me how can i do this. If you provide me code for this then it's really a great help for me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900