Filewatcherを使用してフォルダ内のファイルを検出し、そのファイルを新しい場所に移動しようとしました。コンソールアプリケーションを使用している間、私はThe process cannot access the file because it is being used by another process
というエラーが発生しています。プロセスが別のプロセスで使用されているため、ファイルにアクセスできません - Filewatcher - C# - Console Application
OnChanged
の方法でFile.Move(f.FullName, System.IO.Path.Combine(@"C:\Users\ADMIN\Downloads\FW_Dest", Path.GetFileName(f.FullName)));
にこのエラーが表示されています。 下記のコードを確認し、この問題を解決してください。前もって感謝します。
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
こんにちは..ソースフォルダに1つのファイルしか置いていません。それでも 'ディレクトリが見つかりませんでした例外が処理されました'エラーです。あなたは助けてください。 – METALHEAD