Filewatcher를 사용하여 폴더에서 파일을 감지하고 파일을 새 위치로 이동하려고했습니다. 콘솔 응용 프로그램을 사용하는 동안 The process cannot access the file because it is being used by another process
으로 오류가 발생합니다.다른 프로세스에서 사용 중이기 때문에 프로세스가 파일에 액세스 할 수 없습니다. Filewatcher - C# - 콘솔 응용 프로그램
File.Move(f.FullName, System.IO.Path.Combine(@"C:\Users\ADMIN\Downloads\FW_Dest", Path.GetFileName(f.FullName)));
에서 OnChanged
방법으로이 오류가 발생합니다. 아래 코드를 확인하고이 문제를 해결해주십시오. 미리 감사드립니다.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Security.Permissions;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Run();
}
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
public static void Run()
{
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = @"C:\Users\ADMIN\Downloads\FW_Source";
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.Filter = "*.*";
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.EnableRaisingEvents = true;
Console.WriteLine("Press \'q\' to quit the sample.");
while (Console.Read() != 'q') ;
}
private static void OnChanged(object source, FileSystemEventArgs e)
{
DirectoryInfo directory = new DirectoryInfo(@"C:\Users\ADMIN\Downloads\FW_Source\");
FileInfo[] files = directory.GetFiles("*.*");
foreach (var f in files)
{
File.Move(f.FullName, System.IO.Path.Combine(@"C:\Users\ADMIN\Downloads\FW_Dest", Path.GetFileName(f.FullName)));
}
}
}
}
'디렉토리 찾을 수 없습니다 예외였다 처리 오류. 도와 주시겠습니까? – METALHEAD
폴더의 모든 파일을 이동하고 있지만 여러 개의 알림이있을 수 있습니다. 변경된 파일을 이동해야합니다. – BugFinder
안녕하세요. 원본 폴더에 하나의 파일 만 넣었습니다. 여전히'Directory Not found Exception was treated' 오류가 발생합니다. 도와 주실 수 있니? – METALHEAD