简体   繁体   中英

File extensions and C#

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.

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.

Use Directory.GetFiles(path, "*.*")

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.

您可以使用Directory.GetFiles重载来包含所有文件。

Directory.GetFiles(path, "*")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM