复制文件时如何防止此System.IO.IOException? [英] How do I prevent this System.IO.IOException when copying a file?

查看:1292
本文介绍了复制文件时如何防止此System.IO.IOException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行以下代码来测试复制目录时,在调用fileInfo.CopyTo方法时会出现System.IO.IOException。错误消息是:进程无法访问文件'C:\CopyDirectoryTest1 \ temp.txt',因为它正由另一个进程使用。



似乎在file1(C:\CopyDirectoryTest1 \ temp.txt)上有一个锁定,它在错误发生的上面几行创建,但如果是这样的话,我不知道如何释放它。任何想法?

 使用System; 
使用System.IO;

namespace TempConsoleApp
{
class program
{
static void Main(string [] args)
{
string folder1 = @C:\CopyDirectoryTest1;
string folder2 = @C:\CopyDirectoryTest2;
string file1 = Path.Combine(folder1,temp.txt);

if(Directory.Exists(folder1))
Directory.Delete(folder1,true);
if(Directory.Exists(folder2))
Directory.Delete(folder2,true);

Directory.CreateDirectory(folder1);
Directory.CreateDirectory(folder2);
File.Create(file1);

DirectoryInfo folder1Info = new DirectoryInfo(folder1);
DirectoryInfo folder2Info = new DirectoryInfo(folder2);

foreach(fileInfo fileInfo in folder1Info.GetFiles())
{
string fileName = fileInfo.Name;
string targetFilePath = Path.Combine(folder2Info.FullName,fileName);
fileInfo.CopyTo(targetFilePath,true);
}
}
}
}


解决方案

File.Create 返回一个打开的 FileStream - 您需要关闭该流。



使用(File.Create(file1)){}

应该这样做。


When I run the following code to test copying a directory, I get a System.IO.IOException when fileInfo.CopyTo method is being called. The error message is: "The process cannot access the file 'C:\CopyDirectoryTest1\temp.txt' because it is being used by another process."

It seems like there's a lock on file1 ("C:\CopyDirectoryTest1\temp.txt") which is created a few lines above where the error is occurring, but I don't know how to release this if so. Any ideas?

using System;
using System.IO;

namespace TempConsoleApp
{
    class Program
    {
        static void Main(string[] args)
        {
            string folder1 = @"C:\CopyDirectoryTest1";
            string folder2 = @"C:\CopyDirectoryTest2";
            string file1 = Path.Combine(folder1, "temp.txt");

            if (Directory.Exists(folder1))
                Directory.Delete(folder1, true);
            if (Directory.Exists(folder2))
                Directory.Delete(folder2, true);

            Directory.CreateDirectory(folder1);
            Directory.CreateDirectory(folder2);
            File.Create(file1);

            DirectoryInfo folder1Info = new DirectoryInfo(folder1);
            DirectoryInfo folder2Info = new DirectoryInfo(folder2);

            foreach (FileInfo fileInfo in folder1Info.GetFiles())
            {
                string fileName = fileInfo.Name;
                string targetFilePath = Path.Combine(folder2Info.FullName, fileName);
                fileInfo.CopyTo(targetFilePath, true);
            }
        }
    }
}

解决方案

File.Create returns an open FileStream - you need to close that stream.

Just

using (File.Create(file1)) {}

should do the trick.

这篇关于复制文件时如何防止此System.IO.IOException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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