0

I currently learning about ASP.NET MVC 4.

I want to display .cs and .cpp files located in a directory on the web page. But somehow I get exception for multiple file type display.

Below line of code gives exception:

string pattern ="*.cs|*.cpp";

Till now I wrote below code:

public ActionResult Contact()
{
    string pattern = "*.cs";
    //string pattern ="*.cs|*.cpp";  // this line does not work  
    ViewBag.Message = "Your contact page.";
    DirectoryInfo dirInfo = new DirectoryInfo(@"f:\");
    List<string> filenames = dirInfo.GetFiles(pattern).Select(i => i.Name).ToList();
    ViewBag.data = filenames;
    return View(filenames);
}

The View code looks like:

@{
    ViewBag.Title = "Contact";
}
<hgroup class="title">
    <h1>@ViewBag.Title.</h1>
    <h2>@ViewBag.Message</h2>
</hgroup>
<table bgcolor="#00FF00">
    @foreach (var item in (List<string>)ViewBag.data)
    {
        <tr>
            <th>@item   <br></th>

        </tr>
    }
</table>
tereško
  • 58,060
  • 25
  • 98
  • 150
Unbreakable
  • 7,776
  • 24
  • 90
  • 171

2 Answers2

0

DirectoryInfo.GetFiles(pattern) only allows a single pattern. It is not like the filter you set when creating a common dialog.

If you want multiple patterns, you can create your own extension method, there are some examples here:: GetFiles with multiple extensions

Community
  • 1
  • 1
paul
  • 21,653
  • 1
  • 53
  • 54
0

You're going to need to do something like this:

        string pattern = "*.cs";
        ViewBag.Message = "Your contact page.";
        DirectoryInfo dirInfo = new DirectoryInfo(@"f:\");
        List<string> filenames = new List<string>();
        foreach (FileInfo f in dirInfo.GetFiles(pattern))
        {
            filenames.Add(f.Name);  //or f.FullName to include path
        }
        pattern = "*.cpp";
        foreach (string f in dirInfo.GetFiles(pattern))
        {
            filenames.Add(f);
        }

        ViewBag.data = filenames;
        return View(filenames);

or, something like this:

        public ActionResult Contact()
    {

        string pattern = "*.cs|*.cpp";
        ViewBag.Message = "Your contact page.";
        DirectoryInfo dirInfo = new DirectoryInfo(@"f:\");
        List<string> filenames = new List<string>();
        string[] temp = pattern.Split('|');
        for (int i = 0; i < temp.Length; i++)
        {
            filenames.AddRange(GetFiles(temp[i]));
        }
        ViewBag.data = filenames;
        return View(filenames);
    }

    public List<string> GetFiles(string pattern) {
        DirectoryInfo dirInfo = new DirectoryInfo(@"f:\");
        List<string> filenames = new List<string>();
        foreach (FileInfo f in dirInfo.GetFiles(pattern))
        {
            filenames.Add(f.Name);  //or f.FullName to include path
        }
        return filenames;
    }

The nice thing about that, is that you'll have an opportunity to sort, and filter the list further before returning it to your view.

Alex T
  • 729
  • 7
  • 15
  • the first method that you have mentioned gives error as line ' foreach (string f in dirInfo.GetFiles(pattern))' : I get red squiggly under the 'foreach' – Unbreakable Jan 30 '15 at 14:57
  • It says cannot convert System.IO.FileInfo to 'string' – Unbreakable Jan 30 '15 at 14:58
  • That was more of an example than anything else. You get back a File object, and File.Name would be the string you're after.. – Alex T Jan 30 '15 at 23:22
  • I updated the code to use FileInfo instead of String. The above code should now run without issues. – Alex T Jan 30 '15 at 23:29