how can set file to hidden and show

sh hamidi 46 Reputation points
2021-10-20T06:59:54.743+00:00

I will create a folder to download the audio file, but I do not want the user to be able to access those files on their mobile phone. I just want it to be played through the app

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,294 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 26,221 Reputation points Microsoft Vendor
    2021-10-21T07:17:20.257+00:00

    Hello,

    Welcome to our Microsoft Q&A platform!

    There are two ways to do ,using Internal Storage or setting the file hidden.

    Internal Storageā€“ this is a portion of the file system that can be accessed only by the application or the operating system.
    I save a text file, you could refer to the following code

    protected override void OnAppearing()  
            {  
                base.OnAppearing();  
                String str = "123456";//test  
                Byte[] byteArray = System.Text.Encoding.Default.GetBytes(str);  
                string fileName = "test.txt";  
                string documentPath = FileSystem.CacheDirectory;  
                string path = Path.Combine(documentPath, fileName);  
                File.WriteAllBytes(path, byteArray);  
            }  
    

    Create a hidden folder simply by adding a '.' to the front of the folder name.
    I save the test.text file by External Storage , you could reder to the following code

     private void Button_Clicked(object sender, EventArgs e)  
                {  
                    String str = "123456";//test  
                    Byte[] byteArray = System.Text.Encoding.Default.GetBytes(str);  
                    string fileName = "test.txt";  
                    String testPath = DependencyService.Get<PathService>().GetPath();  
                    var folderPath = testPath + "/.test";  
                    if (!Directory.Exists(folderPath))//creat a folder to save  
                    {  
                        DirectoryInfo dirInfor = new DirectoryInfo(folderPath);  
                        dirInfor.Create();  
                    }  
                    var filePath = System.IO.Path.Combine(folderPath, fileName);  
                    File.WriteAllBytes(filePath, byteArray);  
                }  
    

    use DependencyService to get path

    public interface PathService  
        {  
            String GetPath();  
        }  
    

    Android

    [assembly: Dependency(typeof(PathServiceAndroid))]  
    namespace AndroidStrogeDemo.Droid  
    {  
        public class PathServiceAndroid : PathService  
        {  
            public string GetPath()  
            {  
                string testPath = Android.App.Application.Context.GetExternalFilesDir("").AbsolutePath;  
                return testPath;  
            }  
        }  
    }  
    

    permission

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

    However, you want to save video, internal storage directories tend to be small , your app should query the free space on the device before writing app-specific files to internal storage. If you create a hidden folder, user still can set "Don't show hidden files" or "Show hidden files". There is not a perfect way to do this, because this is the feature of Android.

    Best Regards,
    Wenyan Zhang


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    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