简体   繁体   English

如何从 Xamarin.Forms 项目中的本地应用程序文件夹中检索文件列表?

[英]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.我在我的 Xamarin.Forms 项目中创建了一个文件夹“MyFolder”,该文件夹内的文件很少。 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?如何从 Xamarin.Forms 项目中的本地应用程序文件夹中检索文件列表?

Try to traverse the resources in the assembly of the app to detect if the resource name contains 'MyFolder'.尝试遍历应用程序集中的资源,检测资源名称是否包含“MyFolder”。 You could get the resources using GetManifestResourceNames method.您可以使用GetManifestResourceNames方法获取资源。

Don't forget to set the Build Action of all the files in the folder to EmbeddedResource .不要忘记将文件夹中所有文件的Build Action设置为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参考: https://stackoverflow.com/a/47185054/11083277

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Xamarin.Forms中,如何从项目的文件夹中读取资产列表? - In Xamarin.Forms, how to read a list of assets from a project's folder? 如何使用 XAML 文件(Xamarin.Forms,Android)的 Resources 文件夹中的 Styles - How to Use Styles from the Resources Folder in XAML Files (Xamarin.Forms, Android) Xamarin.Forms:Android项目生成错误-AppData \\ Local \\ Xamarin文件夹内缺少文件 - Xamarin.Forms: Android project Build Error - Missing Files inside AppData\Local\Xamarin Folders 在Xamarin.Forms中打开本地HTML文件不在资产文件夹中查看 - Open Local HTML files in Xamarin.Forms Web View NOT in Assets Folder 如何从 Xamarin.Forms 应用程序执行 SAML 2.0 身份验证 - How to do SAML 2.0 Auth from Xamarin.Forms application 如何在Xamarin.Forms项目中连接WebServiceURL? - How to Connect WebServiceURL in Xamarin.Forms project? 如何更改Xamarin.Forms中的应用程序图标? - How to change application icon in Xamarin.Forms? 有没有办法将列表从 android 项目传递到 xamarin.forms 项目中的 MainPage - Is there a way to pass list from android project to MainPage in xamarin.forms project Xamarin - 有没有办法从本地项目中通知Xamarin.Forms? - Xamarin - is there a way to notify Xamarin.Forms from a native project? 将项目从Xamarin.Android传输到Xamarin.Forms - Transfer project from Xamarin.Android to Xamarin.Forms
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM