代码之家  ›  专栏  ›  技术社区  ›  Ankit Jain

在Xamarin UWP应用程序中从远程网络共享打开文件

  •  0
  • Ankit Jain  · 技术社区  · 7 年前

    有没有办法在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);
     }
    

    如果用户点击 路径 然后文件将以读取模式显示。

    2 回复  |  直到 7 年前
        1
  •  1
  •   Nico Zhu    7 年前

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

    UWP已提供 broadFileSystemAccess 能够使用中的API访问更广泛的文件 Windows.Storage namespace 。您需要添加受限 BroadFileSystem访问 访问前的能力。

    <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标准

    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; }
     }
    

    使生效 IFileAccess 本机UWP项目中的接口。

    FileAccessImplementation文件访问实现

    [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");
    
        2
  •  0
  •   Grisha    7 年前

    如果您有文件的名称和路径,只需读取其内容即可。您不需要文件选择器。

    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();
    
              }