In the app that I am developing I need to create a different button for each file contained into a directory in Xamarin.Forms, and each button Text needs to correspond to a different file name. Example: in my Xamarin.Forms project I have a folder called "Sounds", in that folder I have 4 mp3 file "Hello.mp3, Theme.mp3, Ring.mp3, Blop.mp3". So in that scenario what I need is to create 4 different buttons with text "Hello.mp3" for button 1, "Theme.mp3" for button 2 etc... So I need to be able to read all the files name in a particular folder. Is there a way to do that in Xamarin Forms?
Asked
Active
Viewed 612 times
1
-
Directory.GetFiles() – Jason Aug 23 '20 at 16:22
-
@Jason I am not sure that the function Directory.GetFiles() will work even after compiling and deploying the app to the device... Because i've seen in documentation that the the path needs to be a string in the form of "c:\", and after deploying the app to an actual device that path will no longer exist in that format – Poli97 Aug 24 '20 at 13:50
-
of course it works - you just have to use a file path appropriate to the system you're running on. – Jason Aug 24 '20 at 13:52
-
@Jason And how can I retrieve it based on different system (iOS/Android)? – Poli97 Aug 24 '20 at 15:39
-
which project are you putting the files in (shared project or platform project)? – Jason Aug 24 '20 at 15:49
-
In a folder in shared project (Forms) – Poli97 Aug 24 '20 at 16:23
-
I don't believe that .NET Standard library project have any mechanism to include files. You could try including them as embedded resources instead of files. – Jason Aug 24 '20 at 16:29
-
And where should I store the files to be able to easily read them? In my app I need to be able to play different sounds from a folder, but I don't know how many audio files are there and lately I may be able to download them from an url (so they won't be preloaded in the app). Any idea? – Poli97 Aug 24 '20 at 16:40
-
To be more clear, I am trying to create like a soundoard application, with some pre loaded audios that will be present in the app since the installation and with some other that can be downloaded lately – Poli97 Aug 24 '20 at 16:42
-
as I already mentioned, you could include them as embedded resources. Or if you really need them as files, put them in each platform project instead of the shared project – Jason Aug 24 '20 at 16:48
-
Ok and is there a way to automatically include them as embedded resources without right clicking each of them, assuming that I will be downloading them from an url afterwards? And then, how will the directory path be called if I store them in shared project? Sorry but it is the first time that I am working with files and file system in Xamarin – Poli97 Aug 24 '20 at 17:01
-
including them as resources shouldn't be any different than including them as files. There are numerous existing questions about how to load embedded resources from a library. It would probably make sense to copy the resources to a writable folder at run time, then you could download your other files to that same folder. – Jason Aug 24 '20 at 17:05
-
Oh right, good suggestion to copy them to a writable folder, thanks. So considering that I have these embedded resources, returning to the main question, how can I read all the names of embedded resources contained into a folder? – Poli97 Aug 24 '20 at 17:18
-
as I said before, this has been asked numerous times. See https://stackoverflow.com/questions/8208289/list-all-embedded-resources-in-a-folder – Jason Aug 24 '20 at 17:20
-
Oh right sorry, thank you, you really helped me a lot! – Poli97 Aug 24 '20 at 17:22
1 Answers
1
Assuming that you are using a StackLayout with name "_stackLayout" in your XAML, you can add this in your constructor of your CS page
foreach (var file in System.IO.Directory.GetFiles({DirectoryPath})
{
AddButton(file.ToString());
};
And then just add this function below the constructor
public void AddButton(string name)
{
_stackLayout.Children.Add(new Button() { Text = name });
}

Saamer
- 4,687
- 1
- 13
- 55
-
I am not sure that the function Directory.GetFiles() will work even after compiling and deploying the app to the device... Because i've seen in documentation that the the path needs to be a string in the form of "c:\", and after deploying the app to an actual device that path will no longer exist in that format – Poli97 Aug 24 '20 at 11:37
-
Place a breakpoint and see what is returned by Directory.GetFiles(). From there’s it’s a piece of cake :) – Saamer Aug 24 '20 at 17:38