2012-10-19 119 views
1

我创建了一个服务,将某个目录中的某些文件类型移动到另一个文件类型,这在本地运行良好,而且在我的网络上运行速度非常快。在不同的网络上,虽然它的工作速度非常慢(500mb文件需要6 1/2分钟),但通过资源管理器复制并粘贴到文件夹中的相同文件在大约30/40秒内完成。C#File.Move服务缓慢通过网络

发生文件移动的代码片段。

currentlyProcessing.Add(currentFile.FullName); 
try 
{ 
    eventMsg("Attempting to move file","DEBUG"); 

    File.Move(oldFilePath, newFilePath); 
    eventMsg("File Moved successfully","DEBUG"); 


} 
catch (Exception ex) 
{ 
    eventMsg("Cannot Move File another resource is using it", "DEBUG"); 
    eventMsg("Move File Exception : " + ex, "DEBUG"); 

} 
finally 
{ 
    if(File.Exists(currentFile.FullName + ".RLK")) 
    { 
     try 
     { 
      File.Delete(currentFile.FullName + ".RLK"); 
     } 
     catch (IOException e) 
     { 
      eventMsg("File Exception : " + e, "DEBUG"); 
     } 

    } 
    currentlyProcessing.Remove(oldFilePath); 
} 

我担心的代码是罚款(因为它按预期工作的其他网络上),所以这个问题可能是网络,在某些形状或形式。有没有人有任何常见的技巧检查,服务作为本地系统(或网络服务)运行,并且似乎没有访问问题。还有什么其他因素会影响这个(网络/硬件除外)。

我想要的是它具有与我在资源管理器中见过的相似的传输速度。任何指针不胜感激。

+0

我认为你需要使用ftp或任何这样的文件传输协议通过网络移动文件.. – dotNETbeginner

回答

0

首先 互相ping通以检查延迟,以便查看问题是全局性的,还是与您的软件有关。

cmd ping 192.168.1.12 -n 10 

如果它在网络上的一个问题,按照此:

  1. 重新启动HUB /路由器
  2. 是在一台PC使用WiFi与低信号?
  3. 是否有任何防病毒功能打开并监控网络活动?

如果没有上述解决您的问题,那么请尝试使用WireShark能够进一步调查该问题。

0

对于这样大的文件,如果可能的话,我会建议将它们压缩起来,尤其是因为网络延迟一直是通过互联网上传/下载文件的一个重要因素。 您可以使用System.IO.Packaging压缩文件。看看这里Using System.IO.Packaging to generate a ZIP file,具体如下:

using (Package zip = System.IO.Packaging.Package.Open(zipFilename, FileMode.OpenOrCreate)) 
     { 
      string destFilename = ".\\" + Path.GetFileName(fileToAdd); 
      Uri uri = PackUriHelper.CreatePartUri(new Uri(destFilename, UriKind.Relative)); 
      if (zip.PartExists(uri)) 
      { 
       zip.DeletePart(uri); 
      } 
      PackagePart part = zip.CreatePart(uri, "",CompressionOption.Normal); 
      using (FileStream fileStream = new FileStream(fileToAdd, FileMode.Open, FileAccess.Read)) 
      { 
       using (Stream dest = part.GetStream()) 
       { 
        CopyStream(fileStream, dest); 
       } 
      } 
     } 

此外,还可以应FTP(如果可能)提到的一些其他用户。查看CodePlex上的Renci SSHNet library