Hello,
Welcome to our Microsoft Q&A platform!
You can use dependenceService to create a folder and a file in device internal storage.
First, create an interface in your PCL.
public interface IMyFile
{
void CreateFileDirectory();
}
Invoke this dependenceService in button click event or other method.
private void Button_Clicked(object sender, EventArgs e)
{
DependencyService.Get<IMyFile>().CreateFileDirectory();
}
Then achieve it in Android folder.
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Util;
using Android.Views;
using Android.Widget;
using JanpanLabel.Droid;
using Java.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Xamarin.Forms;
[assembly: Dependency(typeof(MyFileService))]
namespace JanpanLabel.Droid
{
class MyFileService : IMyFile
{
public void CreateFileDirectory()
{
// throw new NotImplementedException();
// var backingFile = Path.Combine(, "count.txt");
File file = new File(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal).ToString(), "/Myfolder/ount.pdf");
if (!file.Exists())
{
if (!file.Mkdirs())
{
Log.Warn("TravellerLog :: ", "Problem creating Myfolder and pdf file");
//ret = false;
}
}
}
}
}
We cannot check the folder and a file directly in VS, but we can use android studio's device file explorer, open the data/data/com.company.packagename/
. this folder and file is created like following screenshot.
Best Regards,
Leon Lu
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.