uncle_scrooge
uncle_scrooge

Reputation: 429

Get files from a folder that I have created in Xamarin.Android

I want get all files from an external storage folder(wall_e_imgs)..Here are codes-

public void getImages()
    {
        var path1 = Android.OS.Environment.ExternalStorageDirectory.AbsolutePath.ToString();
        string path = System.IO.Path.Combine(path1, "wall_e_imgs");
        //var files= System.IO.Directory.GetFiles(Android.OS.Environment.ExternalStorageDirectory.ToString() + "wall_e_imgs");
        //var files = System.IO.Directory.GetFiles(path);
        //string path = Android.OS.Environment.ExternalStorageDirectory.ToString() + "/wall_e_imgs";
        //File directory=new File(path);
        Java.IO.File directory = new Java.IO.File(path);
        Java.IO.File[] files = directory.ListFiles();//always count is 0 even though there are lot files there

        foreach (var i in files)
        {
            FileInfo info = new FileInfo(i.Name);
            if (info.Name.Contains("Wall_e"))
            {
                di.Add(new DownloadedImages { Path1 = info.DirectoryName, Name1 = info.FullName });
            }
        }

    }

But it always give 0 files even though there are lot of files.

Upvotes: 2

Views: 9980

Answers (3)

pollaris
pollaris

Reputation: 1321

ffilelist will contain a list of mp3 files in "/storage/emulated/0/Music/"

    string phyle;
    string ffilelist = "";
    public void listfiles()
    {
        try
        {
            var path1 = "/storage/emulated/0/Music/";
            var mp3Files = Directory.EnumerateFiles(path1, "*.mp3", SearchOption.AllDirectories);           
            foreach (string currentFile in mp3Files)
            {
                phyle = currentFile;
                ffilelist = ffilelist + "\n" + phyle;
            }
            //playpath(phyle); // play the last file found
        }
        catch (Exception e9)
        {              
            Toast.MakeText(ApplicationContext, "ut oh\n"+e9.Message , ToastLength.Long).Show();
        }
    }

Upvotes: 0

Shyju M
Shyju M

Reputation: 9933

Try this

var folder = Android.OS.Environment.ExternalStorageDirectory + Java.IO.File.Separator + "yourfoldername";
                if (!Directory.Exists(folder))
                    Directory.CreateDirectory(folder);

                var filesList = Directory.GetFiles(folder);
                foreach (var file in filesList)
                {
                    var filename = Path.GetFileName(file);
                }

Upvotes: 4

Frauke
Frauke

Reputation: 1582

Try something like this:

// Use whatever folder path you want here, the special folder is just an example
string folderPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), "wall_e_imgs");
if (Directory.Exists(folderPath))
{
    var files = Directory.EnumerateFiles(folderPath);
    foreach (var file in files)
    {
        // Do your stuff
    }
}

Please note that this uses the Directory class from System.IO, not Java.IO

Upvotes: 1

Related Questions