需要从C#中的Web应用程序转移多个远程计算机上的文件夹 [英] need to transfer folder on multiple remote machine from web application in C#

查看:53
本文介绍了需要从C#中的Web应用程序转移多个远程计算机上的文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从asp.net Web应用程序在客户端计算机上创建文件夹,然后使用C#将多个文件从服务器传输到此文件夹到客户端计算机.我使用过C#Windows模拟,通过它我可以创建文件夹并传输文件,直到并且除非检查了Microsoft网络(网络协议)的文件和打印机共享,但是当我取消选中客户端计算机上的此网络协议时,我的代码给我错误找不到网络路径".请告诉我,如果您还有其他选择可以使用asp.net Web应用程序在多台客户端计算机上传输文件夹.我在这个问题上停留了一个多星期.

请在下面找到我的代码:-

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);
            }
        }

推荐答案

\ hp; 如果(Directory.Exists(Drv_FolderPath)) { //DeleteDir(); } //Drv_FolderPath = @"\\" + client_IPAddress + @"\ 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; //在包含所有文件及其路径的哈希表上循环 foreach(Fod_Files.Values中的字符串f) { FileName = f.Split(''\\''); 字符串Dest_Path = Drv_FolderPath + @"\" + FileName [i]; 如果(!File.Exists(Dest_Path)) { //它将在客户端计算机上复制文件 System.IO.File.Copy(f,Dest_Path); } } //utl.putLogData(DateTime.Now.ToString()+驱动程序设置已成功复制"); } 别的 { } } 抓住(前例外) { } 最后 { //停止模拟并还原为进程标识 如果(impersonatedUser!= null) impersonatedUser.Undo(); ////释放令牌 if(令牌!= IntPtr.Zero) CloseHandle(令牌); } }
\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); } }


这是一个安全问题.服务器应用程序需要在客户端上创建任何内容的权限,因此您必须经过正确的路线或创建可以为您完成工作的客户端应用程序.
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.


这篇关于需要从C#中的Web应用程序转移多个远程计算机上的文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆