代码之家  ›  专栏  ›  技术社区  ›  John Wooten

如何在web app中读取另一个驱动器目录中的文件列表?

  •  0
  • John Wooten  · 技术社区  · 7 年前

     public void OnGet()
        {
            StringBuilder logContent = new StringBuilder();
            string[] files = null;
            string directory = @"Q:\logs";
            string reason = "Undefined";
            try
            {
                files = Directory.GetFiles(directory, "*.csv");
            }
            catch (Exception ex)
            {
                reason = ex.Message;
            }
            if (files != null && files.Length > 0)
            {
                logContent.Append(@"<select>");
                foreach (string file in files)
                {
                    string aFile = file.Substring(file.LastIndexOf(Path.DirectorySeparatorChar) + 1);
                    logContent.Append("<option value=\"" + aFile + "\">" + aFile + "</option>");
                }
                logContent.Append(@"</select>");
    ...
      else
        {
            logContent.Append("<h3>An Exception occurred: " + reason + "</h3>");
        }
    
        Message = logContent.ToString();
    }
    

    当页面运行时,我得到异常

    驱动器在那里,如果我做一个 dir Q:\logs 在C驱动器的命令提示符中,它显示内容。

    我错过了什么?

    1 回复  |  直到 7 年前
        1
  •  0
  •   Lev    7 年前