デスクトップのWindowsストアアプリを開発しようとすると、レンガの壁にぶつかったようです。私は、別のアプリケーションが開いている大きな(100 MB以上の)ログファイルを開いて、ファイルに書き込まれている最新のイベントをリアルタイムで処理しようとしています。定期的に、非サンドボックスのC#でUWP - 別のアプリケーションが開いている場合にファイルを開くことができません
が、これは非常に簡単です:私は別のアプリケーションで使用中のファイルを開こうとするたび
System.IO.FileStream stream = File.Open("LOGFILE PATH HERE", System.IO.FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
は残念ながら、UWPで、私は「UnauthorizedAccessException」を取得します。私は見つけることができるすべての組み合わせですべてのAPIを試しましたが、運がなかったので、ここでいくつかの提案をしてきました。
私が試したことのいくつか:
Windows.Storage.Pickers.FileOpenPicker picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.List;
//Prompt the user to open the log file:
Windows.Storage.StorageFile logFile = await picker.PickSingleFileAsync();
picker.FileTypeFilter.Add(".txt");
//This won't work in any case, because it doesn't use the handle that the user picked,
// so the UWP sandboxing blocks it:
new FileStream(logFile.Path, FileMode.OpenOrCreate, FileAccess.Read);
//EDIT: These don't work if the file is open either, I must have made a mistake earlier
await FileIO.ReadBufferAsync(logFile);
await FileIO.ReadLinesAsync(logFile);
//These work if the file is not open by another app, but fail if another app has the file open
await logFile.OpenAsync(Windows.Storage.FileAccessMode.Read);
await logFile.OpenStreamForReadAsync();
クイックリプロ:
オープンPowerShellウィンドウ、および自分のホームディレクトリにオープン "のtest.txt" を開催するには、次のコマンドを実行します。
この投稿を参照してください:http://stackoverflow.com/questions/4400517/how-can-i-read-a-file-even-when-getting-an-in-use-by-another-process -exception – Sparrow
これは非UWPアプリケーションの場合のみです。この場合は動作しません。これが私が試した最初のものです。私のコメントではうまくいかなかったことの例として、このコードがあります。*( –
Man、これは欲求不満です.. System.Diagnostics.Stopwatchによると、ReadBufferAsyncは100MBのログファイルを100ms読み込みます。 SSDを搭載した超高速のdevマシンでは、実際にはファイル全体がバッファリングされているようです。 一方、まだ開かれていないファイルでは、StorageAsFileのOpenAsync(...)メソッドは2ms以下です。 。Fiddlesticks !!! –