2012-06-04 91 views
0

这是一个web应用程序 我有2个pc的:答:192.168.1.200和B:192.168.1.201,我想从A复制到B,这个代码工作是单个PC,但它不工作在网络中。在本地网络中复制文件

protected void Button1_Click(object sender, EventArgs e) 
{ 
    string sourcePath = @"D:\Source\"; 

    string[] filePaths = Directory.GetFiles(sourcePath, "*.txt"); 
    foreach (string a in filePaths) 
    { 
     CopyFiles(a, a.Replace("D:\\Source\\", "D:\\Source1\\New\\")); 
     //CopyFiles(a, a.Replace("D:\\Source\\", "192.168.1.201\\Source1\\New\\")); 
    } 

} 

private bool CopyFiles(string Source, string Destn) 
{ 
    try 
    { 
     if (File.Exists(Source) == true) 
     {   
      File.Copy(Source, Destn); 
      return true; 
     } 
     else 
     { 
      Response.Write("Source path . does not exist"); 
      return false; 
     } 
    } 
    catch (FileNotFoundException exFile) 
    { 
     Response.Write("File Not Found " + exFile.Message); 
     return false; 
    } 
    catch (DirectoryNotFoundException exDir) 
    { 
     Response.Write("Directory Not Found " + exDir.Message); 
     return false; 
    } 
    catch (Exception ex) 
    { 
     Response.Write(ex.Message); 
     return false; 
    } 
} 
+0

如果要将这些文件粘贴到其他计算机桌面上,我将使用IPv4地址,然后提供如下路径:C:\\ Users \\ ASUS \\ Desktop \\? https://stackoverflow.com/a/10883763/9087709 –

回答

0

尝试用:

CopyFiles(a, a.Replace("D:\\Source\\", "\\192.168.1.201\\Source1\\New\\")); 

您还需要确保该文件夹源1在B共享,而且你必须把它写访问。

0

您是否在接收机上创建了Windows共享“Source1”? 如果你没有,我会尝试将其安装到您的发送者机器上的代码更改为:

CopyFiles(a, a.Replace("D:\\Source\\", "\\\\192.168.1.201\\Source1\\New\\")); 
0

你必须被允许在目标机器上写。这里可以使用一轮工作,您可以创建一个指向网络位置的虚拟驱动器,例如Z :.现在你可以使用本地符号。但在此之前,确保远程PC上的权限。

+0

我得到错误... System.Security.SecurityException:提供的名称不是一个正确形成的帐户名称。 – emsk

+0

你使用任何网络证书!看来你不能在远程机器上登录。 –

+0

AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal); WindowsIdentity identity = new WindowsIdentity(“192.168.1.201 \\ abc”,“12344”); WindowsImpersonationContext context = identity.Impersonate(); – emsk