简体   繁体   English

文件扩展名和C#

[英]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. 我是C#和ASP.NET的新手,我试图从目录中读取文件,以便在页面上显示它们。 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. 我遇到的问题是,我尝试读取的文件来自专有的电话录音软件,并且文件没有扩展名,因此当我尝试使用Directory.GetFiles()读取目录中的文件时directoryName)我什么也没有得到,好像目录是空的。 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. 我将检查目录是否正确,IIS(您的应用程序的应用程序池帐户)是否有权访问目录中的文件以及该目录中是否有文件。

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. 我也同意克里斯的观点,IIS可能没有访问目录列出文件的权限。

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

Directory.GetFiles(path, "*")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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