Click here to Skip to main content
15,889,883 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how do i get files from folder example("/products") and in that folder is files that are xml then need to check if they are actually xml files then put those xml files into list object in static memory or convert it to static in memory list using asp.net mvc c#

What I have tried:

tried some research i am totally new to asp.net mvc c#
Posted
Updated 7-Jan-19 21:58pm
v2

 
Share this answer
 
Comments
Member 13935631 8-Jan-19 3:48am    
once i have those files how do i convert those files to object list
Maciej Los 8-Jan-19 3:57am    
EnumerateFiles method returns IEnumerable of string. So, you can easily convert it into Listof object. All you need to do is to use Cast method.
Good luck!
Hi,

Please find below code.you can use it in ur own way.

  static List<string> _list;
      list = new List<string>();
  foreach (string file in Directory.GetFiles(baseFolderPath, "*.xml"))
{
 _list.Add(file);
}
 
Share this answer
 
Comments
Member 13935631 8-Jan-19 4:09am    
this can be done in a controller method using mvc right?
Prasad Nikumbh 8-Jan-19 4:16am    
yes you can use the above code in your own way in controller action method.
CHill60 8-Jan-19 12:30pm    
Seems a very long winded way of doing
List<string> _list = Directory.GetFiles(baseFolderPath, "*.xml").ToList();
There is also an error with
list = new List<string>();
Prasad Nikumbh 9-Jan-19 0:17am    
I have commented there ..you can use it in ur own way.I think the person added the query might have got it..

If you are new to c# development.

Please see the below corrected code:

_list = new List<string>();

This is you have to create instance at the time when u need it.

and globally you can declare like this:
static List<string> _list;

And one thing,this is just one way.you can use it any other way as in MVC we can use in our Business Layer or Action Method also.


Thanks
CHill60 9-Jan-19 2:34am    
If I was new to c# I wouldn't have spotted the bug nor known that the loop was unnecessary :laugh:

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900