Ankit Jain
Ankit Jain

Reputation: 333

Open a file from Remote Network Share in Xamarin UWP application

Is there any way to open a file from Remote Network Share in Xamarin UWP Application. ?

We tried with Xamarin File Picker, but it includes user to select the file.

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

We want that If the user clicks on the Path then the file will display in a Read Mode.

Upvotes: 0

Views: 758

Answers (2)

Nico Zhu
Nico Zhu

Reputation: 32775

Is there any way to open a file from Remote Network Share in Xamarin UWP Application. ?

UWP has provided broadFileSystemAccess capability to access broader file with APIs in the Windows.Storage namespace. You need add the restricted broadFileSystemAccess capability before access.

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

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

If you want to get the file in the .NET Standard, you need create a DependencyService.

Create file access interface in your .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; }
 }

Implement IFileAccess interface in native UWP project.

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

Usage

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

Upvotes: 1

Grisha
Grisha

Reputation: 723

If you have the name and the path of the file, just read its contents. You don't need 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();

          }

Upvotes: 0

Related Questions