Reputation: 39
I am new to C# and ASP.NET and I am trying to read the files from a directory in order to display them on a page. The issue that I am running into is that the files that I am trying to read are from a proprietary phone recording software and the files do not have an extension, hence when I am trying to read the files in the directory using Directory.GetFiles(directoryName) I get nothing in return as if the directory is empty. However when I use a different directory where files have extensions I am able to get the list of the files.
Any ideas?
Code sample
string path = "C:\\Users\\directoryName";
foreach (string filename in Directory.GetFiles(path))
{
FileInfo file = new FileInfo(filename);
string name = file.Name;
Response.Write(name);
}
Thank you in advance for any help provided.
Upvotes: 0
Views: 258
Reputation: 19852
I was a little skeptical that it wouldn't work on extensionless files. So I've tried the code you provided in a console application on a test directory with extensionless files. It works fine for me, I don't believe that the fact it doesn't have an extension is the issue.
I would check that the directory is correct, that IIS (that application pool account of your application) has permission to access the files in the directory and there are files in that directory.
Upvotes: 3
Reputation: 414
If you want to get only files that have no extension you can do the following:
foreach (String sFile in Directory.GetFiles("PATH", "*."))
{
Responce.Write(System.IO.Path.GetFileName(sFile));
}
I also agree with Chris, IIS might not have permissions to access the directory to list the files.
Upvotes: 1
Reputation: 30728
You can use Directory.GetFiles overload to include all the files.
Directory.GetFiles(path, "*")
Upvotes: 0