1

I have a simple question, but that it's driving me crazy. I have a folder with a lot of JSON files inside, I need just to open those files one by one and do things. So, I need to open the first one, read it and do something then go to the second one etc. until the last.

Here the code I tried, searching on web:

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);

But I keep gettin error DirectoryNotFoundException: Could not find a part of the path on the browser. I'm using asp.net CORE 3 and visual studio 2019.

itminus
  • 23,772
  • 2
  • 53
  • 88
Rob None
  • 479
  • 2
  • 9
  • 22

2 Answers2

0

If you want to get the files from your desktop, you could use this instead:

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

But you probably got access right problems

Jeroen van Langen
  • 21,446
  • 3
  • 42
  • 57
0
  1. As others suggest, it's better to move the files inside the solution. For example, you could copy those files into the project folder:

    ├───YourApp.csproj                      # your `*.csproj`
    ├───Controllers/
    ├───Models/
    ├───Views/
    ├───wwwroot/
    ├───MM/                                 # your target folder 
    │   └───VM/
    │       └───VM DB/
    │           └───nv - master/
    │               └───nvd/
    |                   └─── *.json
    
  2. Apart from that, don't forget to add a configuration in your *.csproj file such that your json files will be copied automatically when you build/publish the app:

      <ItemGroup>
        <None Update="MM\**\*.*">
          <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
        </None>
      </ItemGroup>
    
  3. Also, it seems that you're using the ASP.NET Core instead of ASP.NET. If that's the case, you could reference this folder by injecting an IWebHostEnvironment so that you invoke webHostEnv.ContentRootPath to get root directory (i.e., the project file direction in source code, and the directory of YourAssembly.dll when published).

    For example, you could inject an IWebHostEnviroment instance in the controller:

    public class HomeController : Controller
    {
        private readonly IWebHostEnvironment _env;
        public HomeController(IWebHostEnvironment env)
        {
            this._env = env;
        }
        public IActionResult Privacy()
        {
            var dir = Path.Combine( this._env.ContentRootPath, @"MM\VM\VM DB\nv - master\nvd");
            string[] filesnumber = Directory.GetFiles(dir, "*.json");
            foreach (string filename in filesnumber)
            {
                var jsonFull = System.IO.File.ReadAllText(filename);
                ...
            }
            return Ok("hello,world");
         }
    }
    
  4. Finally, in case you don't like dependency injection, or cannot use dependency injection, you could use a path relative to current assembly as below:
     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");
    
    Be careful this is not the safest way. It is suggested that we should use the DI approach (IWebHostEnviroment) if possible.
itminus
  • 23,772
  • 2
  • 53
  • 88