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; }
    }
}
1

2 Answers 2

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;
Sign up to request clarification or add additional context in comments.

6 Comments

System.Collections.Generic.List`1[System.String] Is the result from this :(
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.
the foreach throws an error about cannot convert string to system.io.directoryinfo
string or var doesnt contain "fullName" so the add line throws error. I just changed it to Path.GetFileName(directory) and it works
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.
|
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.