2
public ActionResult Directories()
    {
        GetFolderInfo folder = new GetFolderInfo();
        {
            string dir = @"C:\";
            foreach (string d in Directory.GetDirectories(dir))
            {
                List<string> ab = new List<string>();
                ab.Add(Path.GetFileName(d));
                folder.FolderInformation = ab.ToString();
            }
        }
        return View(folder);
    }

Ok so in my views, i call this but i do not get correct list. I want this method to show me all the file names in the folder 'dir' and display it in my test website

    @model TestMVC.Models.GetFolderInfo

@{
    ViewBag.Title = "Directories";
}

<h2>Directories</h2>

<table>
    <tr>
        <td>
            @Model.FolderInformation  
        </td>
    </tr>
</table>

this above is my views for the method and my model is

namespace TestMVC.Models
{
    public class GetFolderInfo
    {
        public string FolderInformation { get; set; }
    }
}
  • you need this http://stackoverflow.com/questions/5031035/mvc-display-files-from-a-folder-in-a-view or http://stackoverflow.com/questions/28231405/how-to-create-view-to-display-list-of-files-in-asp-net-mvc-4-project – Amitd Apr 20 '17 at 16:49

2 Answers2

2

Based off of your structure, you'll may want to adjust your model to actually store a collection of strings that represent your paths if you want to be able to iterate through them in your View:

public class GetFolderInfo
{
    public IEnumerable<string> FolderInformation { get; set; }
}

This will allow you to directly set the paths within your existing code:

GetFolderInfo folder = new GetFolderInfo();
string dir = @"C:\";

// Store your directory paths
List<string> directories = new List<string>();
foreach (var directory in Directory.GetDirectories(dir))
{
    // Add the directory path to your collection
    directories.Add(directory);
}

// Set your paths
folder.FolderInformation = directories;

Now since you have a collection, you can simply iterate through them within your view:

<h2>Directories</h2>

<table>
    @foreach (var directory in Model.FolderInformation)
    {
        <tr>
            <td>
                @directory
            </td>
        </tr>
    }
</table>

If you wanted to get a bit more complex and store the actual Directory and it's underlying files, then you'll need to iterate through the files within that directory as well within your loop:

// Store your directory paths
List<string> files = new List<string>();
foreach (var directory in Directory.GetDirectories(dir))
{
    var actualDirectory = new DirectoryInfo(directory);
    foreach (FileInfo file in actualDirectory.GetFiles())
    {
         files.Add(file.FullName);
    }
}

folder.FolderInformation = files;
Rion Williams
  • 74,820
  • 37
  • 200
  • 327
  • System.Collections.Generic.List`1[System.String] Is the result from this :( –  Apr 20 '17 at 16:55
  • The result where? You need to use the code that I provided in your View as well to properly iterate through the objects within your model. – Rion Williams Apr 20 '17 at 16:58
  • the foreach throws an error about cannot convert string to system.io.directoryinfo –  Apr 20 '17 at 17:04
  • string or var doesnt contain "fullName" so the add line throws error. I just changed it to Path.GetFileName(directory) and it works –  Apr 20 '17 at 17:08
  • Hmmm, I had thought that `GetDirectories()` returned a `Dictionary[]`, but it seems it's a string array. If that's the case, you'll need to resolve the dictionary to iterate through its files. See the second example above. – Rion Williams Apr 20 '17 at 17:11
  • It depends on what your `Directory` object was. If it is a [`DirectoryInfo`](https://msdn.microsoft.com/en-us/library/system.io.directoryinfo(v=vs.110).aspx) object, then you can directly access the folders via the `GetDirectories()` method. Otherwise, if it is just a [`Directory`](https://msdn.microsoft.com/en-us/library/system.io.directory(v=vs.110).aspx), then you need to resolve each individual one so that you can iterate through the files. – Rion Williams Apr 20 '17 at 17:15
0

Just adding another easier and efficient option:

You should use Directory.EnumerateDirectories. This will work same as GetDirectories. The difference is noted in this SO Answer.

More information on EnumerateDirectories is in this MSDN article.

Community
  • 1
  • 1
Pratik Gaikwad
  • 1,526
  • 2
  • 21
  • 44