我正在开发一个项目,以在 UWP 应用程序中重用我的旧 .NET 库之一。我读了几篇关于 .NET 2.0 标准库。
https://learn.microsoft.com/en-us/windows/uwp/porting/desktop-to-uwp-migrate
我按照该文章中的步骤创建了一个新的 .NET Standard 2.0 库,将旧源文件复制到新库中 库项目并安装了缺少的 NuGet 包并编译了该项目,没有任何错误。
下一步是创建 UWP 应用并添加库项目作为引用。
我的库正在将文件从一种格式转换为另一种格式。所以它使用System.IO和System.Xml.Serialization 命名空间。我知道 UWP 和 Windows 应用商店应用程序中的文件读取和写入的安全设置已更改,并且此 导致一些问题。
我正在打开这样的文件
FileOpenPicker fileOpenPicker = new FileOpenPicker();
fileOpenPicker.SuggestedStartLocation = PickerLocationId.Desktop;
fileOpenPicker.FileTypeFilter.Clear();
fileOpenPicker.FileTypeFilter.Add(".txt");
StorageFile file = await fileOpenPicker.PickSingleFileAsync();
// and here I am calling my Library
var doc = ParserFactory.Parse(file.Path);
// and that's what my library is trying to do
....
var myfile = File.OpenRead(filename)
// File.OpenRead throws a Security Exception.
我以这种方式设计了我的库的界面,您必须传递文件路径,并且库本身将处理 休息。
那么有人知道避免安全异常的解决方案吗?我真的很感激不要重写我的库和/或 使用 Windows.Storage 命名空间“污染”我的库。
提前致谢
最佳答案
我的建议是为 ParserFactory.Parse(Stream stream)
创建重载并使用 StorageFile
打开 Stream
:
var file = await fileOpenerPicker.PickSingleFileAsync();
var stream = await file.OpenStreamForReadAsync();
var doc = ParserFactory.Parse(stream);
不确定您的 Parse
函数如何工作,但希望您关心的是 Stream
实现,而不是 FileStream
。
关于c# - 移植 .NET 库以便在 UWP 应用程序中重用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47526822/