File.Move问题.Net [英] File.Move issue .Net

查看:114
本文介绍了File.Move问题.Net的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,我有一个问题,也许有人可以帮忙。



基本上我有一个应用程序将100个文件从一个本地服务器传输到另一个本地服务器,每隔一段时间(并非所有时间)我都会看到一个问题,即System.IO.File.Move只将文件复制到目标位置并保留原始文件。下面的代码是我用来移动文件的代码,移动前文件肯定不存在于目标位置。



Hey guys, I have an issue that maybe someone can help with.

Basically I have an application that transfers 100's of files from one local server to another, every now and again (not all the time) I see an issue where System.IO.File.Move only copies the file to the destination location and leaves the original. The code below is the code I use for moving the file, The file definitely does not exist in the destination location before the move.

if (!FileInUse(FullFileName, ref sMessage))
{
        File.Move(FullFileName, fullArchiveFileLocation);
        bArchiveSucceeded = true;
}
else
{
  EventLogBusinessLogic.CreateEventLog("Move Failed", "Move failed for file " + FullFileName + " Message:  " + sMessage, DateTime.Now, eEventType.None);
                               Thread.Sleep(1000); //sleep for a second before trying again
}


static bool FileInUse(string path, ref string Message)
       {
           try
           {
               //Just opening the file as open/create
               using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate))
               {
               }
               return false;
           }
           catch (IOException ex)
           {
               //check if message is for a File IO
               Message = ex.Message.ToString();
               return true;
           }
       }





任何关于为什么会发生这种情况的想法或有任何人看过这个以前,非常感谢任何帮助。



Any ideas as to why this is happening or has anyone seen this before, any help is very much appreciated.

推荐答案

可能是这种情况(引自MSDN):

This might be the case (quote from MSDN):

如果您尝试跨磁盘卷移动文件并且该文件正在使用中,则该文件将复制到目标,但不会从源中删除。

If you try to move a file across disk volumes and that file is in use, the file is copied to the destination, but it is not deleted from the source.


Things并不总是它们看起来像Windows文件系统,有一段时间我已经注意到,即使文件夹是空的根据Windows,你仍然可以不删除文件夹。



您可以对Windows进行API调用,询问所有锁定文件的列表,窗口将回复不完整的列表。



在封面窗口下复制整个文件和文件管理器使用webdav复制文件时首先下载文件,然后开始运行绿色进度条,因为它将文件从临时位置复制到新位置。



尝试在代码中使用奇怪的Thread.sleep(),以便在运行时让窗口赶上紧密循环
Things are not always what they seem when it comes to the windows file system and some time i have noticed that even when a folder is empty according to windows that you still cannot delete the folder.

You can make an API call to windows to ask for a list of all the files that are locked and windows will reply with an incomplete list.

Under the cover windows is copying files all over the place and file manager when using webdav to copy files first downloads the file and then starts running the green progress bar as it copies the file from a temp location to the new location.

try using the odd Thread.sleep() in your code to lets windows catch up if you are running a tight loop


只是添加一些文件使用数字证书,如C:\ Windows \ System32 \TsWpfWrp.exe,你将会遇到各种各样的麻烦。 />


我只是试图拿一份System32而且windows文件管理器出现了很多错误,因为文件被锁定但是它不是一个非常好的系统时唯一的方法是复制此文件夹是使用som启动像Tails一样复制
Just to add some files use a digital certificate like C:\Windows\System32\TsWpfWrp.exe and you will get all sorts of trouble playing with them.

I just tried to take a copy of System32 and windows file manager came up with lots of errors because the files are locked but its not a very good system when the only way to copy this folder is to boot up using something like Tails to take a copy


这篇关于File.Move问题.Net的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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