尝试为桌面开发Windows应用商店应用程序时,我似乎遇到了困难。我试图打开另一个应用程序已打开的大型(100+ MB)日志文件,并在写入文件时对最新事件进行实时处理。UWP - 如果其他应用程序打开文件,则无法打开文件
通过定期,非沙盒C#中,这是非常简单的:
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”:
$f = [System.IO.File]::Open("test.txt", [System.IO.FileMode]::OpenOrCreate, [System.IO.FileAccess]::Write, [System.IO.FileShare]::ReadWrite);
看到这篇文章: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也需要100ms才能读取一个适中的100MB日志文件同时,对于尚未打开的文件,StorageFile的OpenAsync(...)方法需要2ms以下的时间,所以它正是我需要的。Fiddlesticks !!! –