【问题标题】:Open a file from Remote Network Share in Xamarin UWP application在 Xamarin UWP 应用程序中从远程网络共享打开文件
【发布时间】:2018-05-11 21:14:49
【问题描述】:

有没有办法在 Xamarin UWP 应用程序中从远程网络共享打开文件。 ?

我们尝试使用 Xamarin 文件选择器,但它包含用户选择文件。

private void OpenFile()
{
    FileData fileData = await CrossFilePicker.Current.PickFile();
    string fileName = fileData.FileName;
    string contents = System.Text.Encoding.UTF8.GetString(fileData.DataArray);
 }

我们希望如果用户点击路径,那么文件将以读取模式显示。

【问题讨论】:

  • 您知道要打开的文件的名称吗?如果没有,您可能需要用户选择它。 FilePicker 在这种情况下运行良好。还是您的问题实际上是关于读取具有某些已知名称的文件的内容?
  • 我们有文件的名称和路径..

标签: xamarin uwp cross-platform filepicker


【解决方案1】:

有没有办法在 Xamarin UWP 应用程序中从远程网络共享打开文件。 ?

UWP 提供了broadFileSystemAccess 功能,可以通过Windows.Storage namespace 中的 API 访问更广泛的文件。您需要在访问之前添加受限的broadFileSystemAccess 功能。

<Package
  ...
  xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities"
  IgnorableNamespaces="uap mp rescap">

...
<Capabilities>
    <rescap:Capability Name="broadFileSystemAccess" />
</Capabilities>

如果您想获取.NET Standard 中的文件,您需要创建一个DependencyService

在你的.NET Standard中创建文件访问接口。

IFileAccess

public interface IFileAccess
 {
     Task<FileData> GetFileStreamFormPath(string Path);
 }
 public class FileData
 {
     public byte[] DataArray { get; set; }
     public string FileName { get; set; }
     public string FilePath { get; set; }
 }

在原生 UWP 项目中实现IFileAccess 接口。

文件访问实现

[assembly: Xamarin.Forms.Dependency(typeof(FileAccessImplementation))]
namespace App6.UWP
{
    public class FileAccessImplementation : IFileAccess
    {
        public async Task<FileData> GetFileStreamFormPath(string Path)
        {
            var file = await StorageFile.GetFileFromPathAsync(Path);
            byte[] fileBytes = null;
            if (file == null) return null;
            using (var stream = await file.OpenReadAsync())
            {
                fileBytes = new byte[stream.Size];
                using (var reader = new DataReader(stream))
                {
                    await reader.LoadAsync((uint)stream.Size);
                    reader.ReadBytes(fileBytes);
                }
            }

            var FileData = new FileData()
            {
                FileName = file.Name,
                FilePath = file.Path,
                DataArray = fileBytes
            };
            return FileData;
        }
    }
}

用法

var file = DependencyService.Get<IFileAccess>().GetFileStreamFormPath(@"\\remote\folder\setup.exe");

【讨论】:

  • Zhu:我们无法在 Pakage.appxmanifest 中设置“广泛的文件系统访问”功能。如果没有在我们的应用程序中进行设置,“var file = await StorageFile.GetFileFromPathAsync(Path)”不会返回任何响应。没有与 Capabilities 相关的可用功能。
  • 您可以通过查看代码 .appxmanifest 文件添加此功能。
  • 右键appxmanifest文件--->点击View Code按钮。
  • 朱:我已经尝试通过添加代码,但它抛出错误“验证错误。错误 80080204:应用程序清单验证错误:应用程序清单 XML 必须有效:第 2 行,第 43 列” .您还可以在新的 Xamarin 跨平台应用程序中重现该案例。
  • 不,它对我有用。请检查您是否丢失了东西。
【解决方案2】:

如果您有文件的名称和路径,只需阅读其内容即可。你不需要 FilePicker。

var filename = @"\\myserver\myshare\myfile.txt";
var file = await StorageFile.GetFileFromPathAsync(filename);
using(var istream = await attachmentFile.OpenReadAsync())
    using(var stream = istream.AsStreamForRead())
          using(var reader = new StreamReader(stream)) {
              var contents = reader.ReadToEnd();

          }

【讨论】:

  • 我不确定,但 StreamReader 不适用于 UWP Xamarin。
  • 我们将 UWP 应用程序定位到 .NET Standard 2.0,并在安装“XPlat.Storage”NuGet 包以使用 Windows.Storage 时。它抛出一个错误“包'XPlat.Storage 1.2.18073.5'是使用'.NETFramework,Version=v4.6.1'而不是项目目标框架'.NETStandard,Version=v2.0'恢复的。这个包可能不是与您的项目完全兼容。0"。任何建议,哪个 NuGet 可以使用 Windows.Storage.StorageFile?
猜你喜欢
  • 2017-04-21
  • 1970-01-01
  • 2010-09-06
  • 2012-12-23
  • 1970-01-01
  • 2013-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多