asp.netasp.net-core-webapi

Getting error in application when I want to display the network drive list


I’ve developed an application that retrieves the list of network drives on a system. The application works perfectly in my local development environment, but when I deploy it to the production server, I encounter the following error:

folder not found

Code:

[HttpPost("ListNetworkDrive")]
public IActionResult ListNetworkDrive([FromBody] string folderPath)
{
   try
   {
       if (Directory.Exists(folderPath))
       {
           var directories = Directory.GetDirectories(folderPath)
               .Select(dir => new
               {
                   Name = Path.GetFileName(dir),
                   Path = dir
               })
               .ToList();

           return Ok(directories);
       }
       else
       {
           Console.WriteLine($"Directory does not exist: {folderPath}");
           return NotFound("Folder not found.");
       }
   }
   catch (Exception ex)
   {
       Console.WriteLine($"Exception accessing directory: {ex}");
       return StatusCode(500, $"Error accessing network drive: {ex.Message}");
   }
}

Solution

  • but when I deploy it to the production server, I encounter the following error

    I deduce this issue might relate to permission issue, my suggestion is that we could set the IIS application which hosting the app to use LocalSystem identity for testing purpose and it looks helpful for solving the issue based on OP's confirmation.