【问题标题】:Move files from one folder to another C# Error File being used by another process将文件从一个文件夹移动到另一个进程正在使用的另一个 C# 错误文件
【发布时间】:2014-01-28 00:49:16
【问题描述】:

我厌倦了将包含特定字符串的文件从一个文件夹复制到另一个文件夹,但它一直给我错误消息 System.IO.IOException: The process cannot access the file 'Z:\Upload\Text-File-1.txt ' 因为它正被另一个进程使用。尝试了一些东西,但没有任何效果。不知道怎么解决。

using System;
using System.IO;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            CheckandMoveFiles();

        }

    private static void CheckandMoveFiles()
    {
        //put filenames in array
        string[] sourcePath = Directory.GetFiles(@"Z:\Upload\");

        string targetPath = @"Z:\Upload\TextFiles\";

            try
            {
                    //Get each filepath and check for string
                foreach (string name in sourcePath)
                    {
                        string d = "Text";

                        if (name.Contains(d))
                        {
                            string fileName = name;
                            string sourceFile = System.IO.Path.Combine(sourcePath.ToString(), fileName);
                            string destFile = System.IO.Path.Combine(targetPath, fileName);
                            using (StreamReader reader = new StreamReader(sourceFile))
                            {

                                File.Copy(sourceFile, destFile, true);
                            }

                        }
                    }

            }
            catch (IOException ex)
            {
                Console.WriteLine(ex); // Write error
            }
            Console.WriteLine("****************************DONE***************************************");
            Console.ReadLine();
        }

}
}

【问题讨论】:

  • 不确定这是否会导致错误 - 但似乎不太可能:您到底为什么要先打开文件进行读取,然后尝试通过文件系统复制它?

标签: c#


【解决方案1】:

看来您的问题出在以下几行:

string sourceFile = System.IO.Path.Combine(sourcePath.ToString(), fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);

因为fileName 已经包含完整路径,而sourcePath 是一个字符串数组,所以sourcePath.ToString() 不会产生您期望的路径。 而是尝试:

string sourceFile = fileName;
string destFile = System.IO.Path.Combine(targetPath, System.IO.Path.GetFileName(fileName));

【讨论】:

    【解决方案2】:
    using (StreamReader reader = new StreamReader(sourceFile))
    {
        File.Copy(sourceFile, destFile, true);
    }
    

    你是开StreamReader去读sourceFile,然后你试试File.Copy()吗?这就是你收到错误的原因

    丢掉 StreamReader,你不需要它

    File.Copy(sourceFile, destFile, true);
    

    会的

    【讨论】:

    • 我有它的另一种方式,它没有工作所以尝试使用语句,看看它是否会在打开文件后关闭我的文件。重试了,失败了
    【解决方案3】:

    应该是这样的:

     if (name.Contains(d))
                    {
                        string fileName = name;
                        string sourceFile = System.IO.Path.Combine(sourcePath.ToString(), fileName);
                        string destFile = System.IO.Path.Combine(targetPath, fileName);
    
                        File.Copy(sourceFile, destFile, true);
    
    
                    }
    

    File.Copy 将文件路径作为参数,而不是指向文件的流。

    【讨论】:

    • 以前有过这样的情况,然后重试了一次,但仍然收到相同的错误消息。
    【解决方案4】:

    试试这个朋友...

    public class SimpleFileCopy
    {
        static void Main()
        {
            string fileName = "test.txt";
            string sourcePath = @"C:\Users\Public\TestFolder";
            string targetPath =  @"C:\Users\Public\TestFolder\SubDir";       
            string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
            string destFile = System.IO.Path.Combine(targetPath, fileName);       
            if (!System.IO.Directory.Exists(targetPath))
            {
                System.IO.Directory.CreateDirectory(targetPath);
            }
            System.IO.File.Copy(sourceFile, destFile, true);
            if (System.IO.Directory.Exists(sourcePath))
            {
                string[] files = System.IO.Directory.GetFiles(sourcePath);           
                foreach (string s in files)
                {               
                    fileName = System.IO.Path.GetFileName(s);
                    destFile = System.IO.Path.Combine(targetPath, fileName);
                    System.IO.File.Copy(s, destFile, true);
                }
            }
            else
            {
                Console.WriteLine("Source path does not exist!");
            }       
            Console.WriteLine("Press any key to exit.");
            Console.ReadKey();
        }
    }
    

    【讨论】:

    • 这真的很好,但是当我尝试获取包含特定字符串的多个文件时,我的问题就来了。我设法做了一个简单的复制,但是当我添加这些条件时,事情发生了变化。
    猜你喜欢
    • 2013-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-04
    • 1970-01-01
    相关资源
    最近更新 更多