繁体   English   中英

将文件从一个文件夹移动到另一进程正在使用的C#错误文件

[英]Move files from one folder to another C# Error File being used by another process

我厌倦了将包含特定字符串的文件从一个文件夹复制到另一个文件夹,但是它一直给我错误消息System.IO.IOException:该进程无法访问文件'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();
        }

}
}
using (StreamReader reader = new StreamReader(sourceFile))
{
    File.Copy(sourceFile, destFile, true);
}

您正在打开StreamReader以读取sourceFile ,然后尝试File.Copy()吗? 这就是为什么您得到错误

失去StreamReader,您不需要它

File.Copy(sourceFile, destFile, true);

会做

应该是这样的:

 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将文件路径作为参数,而不是指向文件的流。

试试这个朋友...

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();
    }
}

您的问题似乎在以下几行中:

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));

暂无
暂无

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

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