简体   繁体   English

IIS中托管时目录中的文件未列出

[英]Files in directory not listing when hosted in IIS

I have used the below code to list the folders and files in a directory. 我已使用以下代码列出目录中的文件夹和文件。 In the web.config file, I have added: 在web.config文件中,我添加了:

<add key="DetailUrlFileExtensions" value=".html,.pdf,.htm" />
fileExtensions = ConfigurationManager.AppSettings["DetailUrlFileExtensions"];
if (fileExtensions != string.Empty)
{
    extentions = fileExtensions.Split(',');
}

var items = Directory.GetFileSystemEntries(dirName);
foreach (var item in items)
{
    if (extentions != null)
    {
        string ext = item.Substring(item.LastIndexOf('.'));
        FileAttributes attr = System.IO.File.GetAttributes(item);
        if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
        {
            list.Add(new Node(Path.GetFileName(item), item, Directory.Exists(item), true));
        }
        else if (extentions.Contains(ext))
        {
            list.Add(new Node(Path.GetFileName(item), item, Directory.Exists(item)));
        }
    }
    else
    {
        list.Add(new Node(Path.GetFileName(item), item, Directory.Exists(item)));
    }
}

But, when running in IIS, it is not working. 但是,当在IIS中运行时,它不起作用。 The folder contains folders and files, and when running in IIS the files are not adding to list. 该文件夹包含文件夹和文件,并且在IIS中运行时,这些文件未添加到列表中。 When running in debug mode from application with 5 folders and 11 files, the foreach block execute 14 times (not 16), but when running from IIS with same folders and files, foreach block execute only 8 times, and no values are added to list. 从具有5个文件夹和11个文件的应用程序以调试模式运行时, foreach块执行14次(而不是16次),但是从具有相同文件夹和文件的IIS运行时, foreach块仅执行8次,并且没有值添加到列表中。

It will list all files when extentions is null . extentionsnull时,它将列出所有文件。

How can I resolve this issue? 我该如何解决这个问题?

Most likely you problem is in IIS_USRS user permission for the folder and its content. 您最有可能遇到的问题是文件夹及其内容的IIS_USRS用户权限。 Possible solutions: 可能的解决方案:

  • try to give IIS_USRS user Full permissions about the folder (reduce then to READ if it work) 尝试为IIS_USRS用户授予有关该文件夹的完全权限(如果可以,则减少为READ)
  • run you application pool under your windows user 在Windows用户下运行应用程序池
  • if security concept does not let you use two solutions mentioned above, develop an internally visible service that could be running on your server under a power user and then in your code get all information you need but over the internally visible service 如果安全性概念不允许您使用上述两个解决方案,请开发一个内部可见的服务,该服务可以在高级用户下在服务器上运行,然后在您的代码中通过内部可见的服务获取所需的所有信息

The following line will fail if the item does not contain a . 如果项目不包含,则以下行将失败. character. 字符。

string ext = item.Substring(item.LastIndexOf('.'));

Since you are processing directories as well as files, I suspect that directories will likely not contain . 由于您正在处理目录以及文件,因此我怀疑目录可能不包含. characters. 字符。

You can safely manipulate file system paths using the System.IO.Path class. 您可以使用System.IO.Path类安全地操作文件系统路径。 Specifically, the above code can be replaced with: 具体而言,以上代码可以替换为:

string ext = Path.GetExtension(item);

The GetExtension method will return string.Empty if there is no extension, rather than throwing an exception. 如果没有扩展名,则GetExtension方法将返回string.Empty ,而不是引发异常。

Alternatively, you can move the retrieval of the file extension to the else block and not perform it for directories: 或者,您可以将文件扩展名的检索移到else块,而不对目录执行它:

FileAttributes attr = System.IO.File.GetAttributes(item);
if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
{
    list.Add(new Node(Path.GetFileName(item), item, Directory.Exists(item), true));
}
else
{
    string ext = item.Substring(item.LastIndexOf('.'));
    if (extentions.Contains(ext)){
        list.Add(new Node(Path.GetFileName(item), item, Directory.Exists(item)));
    }
}

However, even in this case I recommend using System.IO.Path since there is no guarantee that file names will have . 但是,即使在这种情况下,我也建议使用System.IO.Path因为不能保证文件名将具有. characters. 字符。

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

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