1
디렉터리 복사 테스트를 위해 다음 코드를 실행하면 fileInfo.CopyTo 메서드가 호출 될 때 System.IO.IOException이 발생합니다. 오류 메시지 : "프로세스가 다른 프로세스에서 사용 중이기 때문에 'C : \ CopyDirectoryTest1 \ temp.txt'파일에 액세스 할 수 없습니다."파일을 복사 할 때이 System.IO.IOException을 방지하려면 어떻게합니까?
file1 ("C : \ CopyDirectoryTest1 \ temp.txt")에 잠금이있는 것처럼 보이는데,이 오류는 오류가 발생하는 곳의 몇 줄 위에 만들어집니다. 그렇지만이 경우이를 해제하는 방법을 모르겠습니다. 어떤 아이디어?
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);
}
}
}
}