简体   繁体   中英

How to retrieve list of files from the local application folder which is in Xamarin.Forms project?

I have one folder "MyFolder" created in my Xamarin.Forms project and there are few files inside that folder. How can I retrieve all the files which are in that folder?

I tried below code but it's giving exception that Folder does not exist.

foreach (var file in System.IO.Directory.GetFiles("\\MyFolder"))
 {

 }

Even the below code is giving the same error.

string folderPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "MyFolder");

 foreach (var file in System.IO.Directory.GetFiles(folderPath))
 {

 }

How to retrieve list of files from the local application folder which is in Xamarin.Forms project?

Try to traverse the resources in the assembly of the app to detect if the resource name contains 'MyFolder'. You could get the resources using GetManifestResourceNames method.

Don't forget to set the Build Action of all the files in the folder to EmbeddedResource .

Check the code:

var assembly = typeof(App).GetTypeInfo().Assembly;
var list = assembly.GetManifestResourceNames();

List<string> fileList = new List<string>();

foreach (var item in list)
{
    if (item.Contains("MyFolder"))
    {
        fileList.Add(item);
    }
}

Refer to: https://stackoverflow.com/a/47185054/11083277

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM