从我在Xamarin.Android中创建的文件夹中获取文件

时间:2017-06-23 14:55:37

标签: c# xamarin xamarin.android

我想从外部存储文件夹(wall_e_imgs)获取所有文件..以下是代码 -

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 });
            }
        }

    }

但即使有很多文件,它总是会提供0个文件。

3 个答案:

答案 0 :(得分:2)

试试这个

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);
                }

答案 1 :(得分:0)

尝试这样的事情:

// 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
    }
}

请注意,这会使用Directory中的System.IO课程,而不是Java.IO

答案 2 :(得分:0)

ffilelist将包含" / storage / emulated / 0 / Music /"

中的mp3文件列表
    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();
        }
    }