简体   繁体   English

移动文件 C# 总是抛出异常

[英]moving files C# always throws an exception

enter image description here在此处输入图像描述

public void MoveFiles(string[]filesDics, string to, FilesErrorDelegate filesError) {
            FileInfo f;
            foreach (string file in filesDics) {
                try {
                    f = new FileInfo(file);
                    f.MoveTo(to + "\\" + f.Name);
                }
                catch (Exception) {
                    bool ifContinue = filesError(file);
                    if (ifContinue) {
                        break;
                    }
                }

            }
        }







and here i am using the function:
private void BtnMove_Click(object sender, EventArgs e) {
            string[] filesNamesArray = GetFilesArray();
            mf.MoveFiles(filesNamesArray, @"C:\Users\76599\Documents\filesManager", delegates[selectedError]);

        }

does anyone know??有人知道吗?? it is very weird becuz that my friend used exactly the same code in their computer and it did work!这很奇怪,因为我的朋友在他们的计算机上使用了完全相同的代码并且它确实有效!

the exception says there is another process using this file.异常表示有另一个进程使用此文件。 but i don't see any other process is using it.但我没有看到任何其他进程正在使用它。

thanks!!!!!!!!!!!!!谢谢!!!!!!!!!!!!!

If another process is locking the file, you won't be able to move the file up until the other process is closed.如果另一个进程正在锁定文件,则在关闭另一个进程之前,您将无法向上移动文件。

You can find which process is locking the file using approach such as: https://thegeekpage.com/how-to-find-out-which-process-is-locking-a-file-or-folder-in-windows-10/您可以使用以下方法找到哪个进程正在锁定文件: https://thegeekpage.com/how-to-find-out-which-process-is-locking-a-file-or-folder-in-windows- 10/

You also need to not have an command prompt, notepad++ or windows explorer open within those directory as they would lock the file.您还不需要在这些目录中打开命令提示符、notepad++ 或 windows 资源管理器,因为它们会锁定文件。 But a lot of other process has the same behavior.但是许多其他进程具有相同的行为。

Alternatively, for files which are locked, instead of moving them, you could copy them and leave the original lock file depending on your needs.或者,对于被锁定的文件,您可以根据需要复制它们并保留原始锁定文件,而不是移动它们。 You could also have a list of files which were locked and attempt to delete them at a future time.您还可以列出已锁定的文件,并在以后尝试将其删除。 It is also possible that locked file can't be copied depending on the type of lock, which the only solution would be to close the process locking the file.根据锁定的类型,也可能无法复制锁定的文件,唯一的解决方案是关闭锁定文件的进程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM