0

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.

Ankit Jain
  • 315
  • 3
  • 18
  • Do you know the name of the file you want to open? If not, you probably need the user to select it. FilePicker works well in this case. Or is your question actually about reading the contents of the file with some known name? – Grisha May 13 '18 at 07:14
  • We have name and path of the file.. – Ankit Jain May 13 '18 at 07:16

2 Answers2

1

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");
Nico Zhu
  • 32,367
  • 2
  • 15
  • 36
  • Zhu: We are unable to set "Broad Filesystem Access" capability in our Pakage.appxmanifest. Without setting up this in our app, "var file = await StorageFile.GetFileFromPathAsync(Path)" not returning any response. No capability is available related to that in Capabilities. – Ankit Jain May 16 '18 at 07:51
  • You could add this capability with view code .appxmanifest file. – Nico Zhu May 16 '18 at 07:59
  • Right click the appxmanifest file ---> Click `View Code` button. – Nico Zhu May 16 '18 at 08:00
  • Zhu: I already tried by adding in code, but it is throwing an error "Validation error. error 80080204: App manifest validation error: The app manifest XML must be valid: Line 2, Column 43". You can also reproduce the case in a new Xamarin Cross-Platform Application. – Ankit Jain May 16 '18 at 08:12
  • No, it works in my side. please check if you has lost something. – Nico Zhu May 16 '18 at 08:17
  • Don’t forget this `IgnorableNamespaces="uap mp uap5 rescap"` – Nico Zhu May 16 '18 at 08:18
  • I have created a new Cross-Platform application, added the same code given, Also added the "xmlns:rescap="http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" IgnorableNamespaces="uap mp uap5 rescap"" in Package.appxmanifest. And Run the Application. Is there anything else that needs to be done. Additional I am using Windows 10, OS Build: 16299.431. – Ankit Jain May 16 '18 at 08:24
  • You need to delete uap5 . – Nico Zhu May 16 '18 at 08:26
  • Zhu: It worked, but now the next problem arises. The element Capabilities in namespace "http://schemas.microsoft.com/appx/manifest/foundation/windows10/restrictedcapabilities" has invalid child element Capability. – Ankit Jain May 16 '18 at 08:32
  • Please refer my code sample and create a new blank [app](https://github.com/ZhuMingHao/App6.git). – Nico Zhu May 16 '18 at 08:34
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/171132/discussion-between-ankit-jain-and-nico-zhu-msft). – Ankit Jain May 16 '18 at 08:54
  • Zhu: After updating the Windows 10 up to Version 1803. It is working fine. Thank You. – Ankit Jain May 18 '18 at 10:00
0

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

          }
Grisha
  • 713
  • 5
  • 13
  • I am not sure but StreamReader doesn't work with UWP Xamarin. – Ankit Jain May 13 '18 at 07:33
  • We are targetting our UWP application to .NET Standard 2.0 and while installing the "XPlat.Storage" NuGet package to use Windows.Storage. It is throwing an error "Package 'XPlat.Storage 1.2.18073.5' was restored using '.NETFramework,Version=v4.6.1' instead of the project target framework '.NETStandard,Version=v2.0'. This package may not be fully compatible with your project. 0". Any suggesstion, which NuGet will work to use Windows.Storage.StorageFile? – Ankit Jain May 14 '18 at 08:58