Arti
Arti

Reputation: 3071

User accessible folder/file in xamarin.forms

I have used System.Environment.SpecialFolder.Personal to create a file and read/ write to that file.

Reference: Working with files

It works as I can read from the file in my app. But I cannot find or browse to that file from my android device.

I want to create a folder and file inside it which should be accessible by the user. User should be allowed to browse to the file. How can I achive this in xamarin.forms?

Upvotes: 2

Views: 4384

Answers (3)

donaldp
donaldp

Reputation: 471

I actually have a solution to this now (sort of). I didn't manage to get Dropbox working yet (Xamarin are working on a new Dropbox nuget package), but I was able to load external files by using the Xamarin.Plugin.Filepicker nuget package (make sure you use that specific one, as a couple of the others don't work as well, and this one doesn't work that great to start with!). I got this working with both Android and UWP (I found this solution as MicroSoft now recommends using a filepicker for accessing external files). I haven't tried iOS yet.

Note: you won't be able to write to the file, but you can import it for processing from where-ever the user has been editing it. Also note that the tostring() doesn't work - it only returns ASCII codes, so you have to explicitly cast the result to char. The data is returned in an array rather than as a stream.

I believe it IS possible to write to the files too, however you would have to write it all yourself - there is no currently available package that does it. See https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/dependency-service/ to get you started.

Upvotes: 1

donaldp
donaldp

Reputation: 471

I've been looking into this, and I'm not sure it can be done in Forms, unless you have some platform-specific code in there as well. Your previous answer was only about Android (even though you clearly asked about Forms). Here's what I have found out so far...

There is a PCLStorage NuGet package which will allow you to do file operations in your PCL. I have used this successfully...

The problem is the files aren't user-accessible. As per doco I initially tried "IFolder RootFolder=FileSystem.Current.LocalStorage;". I have read if you use external storage (which isn't necessarily actually external, but is "external" to your home folder) then it is user-accesible, and through intellisense I found there was a RoamingStorage option. I tried this, and it worked in UWP, but came up with an unhandled exception on Android,so I'm not sure how to resolve that situation (other than diving into platform-specific code, which kinda defeats using PCLStorage in the first place).

I've also been looking at Dropbox to facilitate the issue, however I'm still not sure how to implement it. I haven't found any decent tutorials on the subject, and trying to work out what to do from just snippets is difficult.

Upvotes: 1

sriman reddy
sriman reddy

Reputation: 773

To Upload the file in external storage of android:

var documentsPath = Android.OS.Environment.ExternalStorageDirectory.ToString();
var filePath = Path.Combine(documentsPath, "test.txt");
System.IO.File.WriteAllText (filePath, "Om Sai Ram");

Add the following permission:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

To create a new folder:

var dir =  new Java.IO.File(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath + "/Sample/");
    if (!dir.Exists ())
        dir.Mkdirs ();
    if(dir.Exists())
        dir.Delete();

Upvotes: 3

Related Questions