【问题标题】:Reading files in a folder one by one in C#在C#中一个一个地读取文件夹中的文件
【发布时间】:2019-12-04 11:16:44
【问题描述】:

我有一个简单的问题,但它让我发疯了。 我有一个文件夹,里面有很多 JSON 文件,我只需要一个一个打开这些文件并做一些事情。 所以,我需要打开第一个,阅读并做一些事情,然后转到第二个等等,直到最后一个。

这是我试过的代码,在网上搜索:

string folderpath = @"C:\Users\rfo\Desktop\MM\VM DB\nv - master\nvd";
            var fixedfolderpath = Environment.ExpandEnvironmentVariables(folderpath);
            string [] filesnumber = Directory.GetFiles(fixedfolderpath, "*.json");
            foreach (string filename in filesnumber)
            {
                var jsonFull = System.IO.File.ReadAllText(filename);

但我总是遇到错误DirectoryNotFoundException: Could not find a part of the path on the browser。 我正在使用 asp.net CORE 3 和 Visual Studio 2019。

【问题讨论】:

  • fixedfolderpath 包含什么?
  • AppPool 可能无权访问用户文件夹。并使用 Server.MapPath。
  • 可能是访问权限问题。将文件夹移动到共享驱动器必须访问 asp.net 用户
  • 请打印fixedfolderpath的输出,并在Directory.GetFiles之前执行if Directory.Exist(fixedfolderpath)
  • @JeroenvanLangen fixedfolderpath 包含大量 JSON 文件。

标签: c# json asp.net-core .net-core


【解决方案1】:

如果您想从桌面获取文件,您可以改用这个:

string path = Path.Combine(
    Environment.GetFolderPath(Environment.SpecialFolder.Desktop), 
    @"MM\VM DB\nv - master\nvd");

但你可能遇到了访问权限问题

【讨论】:

  • “如果您想从桌面获取文件” - 不,您不希望在可能在 IIS 中运行的 ASP.NET 应用程序中这样做。默认情况下,您的应用程序池用户没有加载桌面文件夹。
  • 不工作,我想我还有权利问题。如果我将它移动到解决方案文件夹下?
  • 这样会更好
【解决方案2】:
  1. 正如其他人所建议的,最好将文件移动到解决方案中。例如,您可以将这些文件复制到项目文件夹中:

    ├───YourApp.csproj                      # your `*.csproj`
    ├───Controllers/
    ├───Models/
    ├───Views/
    ├───wwwroot/
    ├───MM/                                 # your target folder 
    │   └───VM/
    │       └───VM DB/
    │           └───nv - master/
    │               └───nvd/
    |                   └─── *.json
    
  2. 除此之外,不要忘记在您的 *.csproj 文件中添加配置,以便在您构建/发布时自动复制您的 json 文件应用:

      <ItemGroup>
        <None Update="MM\**\*.*">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
      </ItemGroup>
    
  3. 另外,您似乎使用的是 ASP.NET Core 而不是 ASP.NET。如果是这种情况,您可以通过注入IWebHostEnvironment 来引用此文件夹,以便调用webHostEnv.ContentRootPath 来获取根目录(即源代码中的项目文件方向,以及发布时YourAssembly.dll 的目录)。

    例如,您可以在控制器中注入 IWebHostEnviroment 实例:

    公共类 HomeController : 控制器 { 私有只读 IWebHostEnvironment _env; 公共 HomeController(IWebHostEnvironment env) { this._env = env; } 公共 IActionResult 隐私() { var dir = Path.Combine(this._env.ContentRootPath, @"MM\VM\VM DB\nv - master\nvd"); string[] filesnumber = Directory.GetFiles(dir, "*.json"); foreach(文件编号中的字符串文件名) { var jsonFull = System.IO.File.ReadAllText(文件名); ... } 返回确定(“你好,世界”); } }
  4. 最后,如果您不喜欢依赖注入,或者不能使用依赖注入,您可以使用相对于当前程序集的路径,如下所示:
     var assemDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
     var dir = Path.Combine(assemDir, @"MM\VM\VM DB\nv - master\nvd");
     string[] filesnumber = Directory.GetFiles(dir, "*.json");
    
    小心这不是最安全的方法。建议尽可能使用 DI 方法 (IWebHostEnviroment)。

【讨论】:

    猜你喜欢
    • 2021-09-04
    • 1970-01-01
    • 2020-05-06
    • 2015-08-12
    • 1970-01-01
    • 1970-01-01
    • 2022-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多