Xamarin forms: How to create folder and a file in device internal storage?

Silvio Sotelo 21 Reputation points
2021-06-01T14:16:46.64+00:00

I am trying to create a directory and pdf file in the internal memory of the device

Developer technologies .NET Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2021-06-02T06:15:33.24+00:00

    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.

    101585-image.png

    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.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.