0

Some context for this issue, I have a VS project that I am running as a service on windows. The project tries to retrieve files from a network folder via \\192.168.97.1\images and then upload them to another server through sftp client connection.

The files I'm trying to retrieve via network folder is actually hosted on a linux machine but I can still access it through file explorer in windows just fine.

public void TransferFiles(){

  ...

  static string path = "\\192.168.97.1\images";
  string filename;

  try{
    using(var sftp = new SftpClient(host, port, username, password)){
      sftp.Connect();
      sftp.ChangeDirectory(destinationDirectory);
  
      if(sftp.IsConnected){
        foreach(string file in Directory.GetFiles(path){
          filename = Path.GetFileName(file);
          Stream stream = File.OpenRead(file);

          sftp.UploadFile(stream, filename);
          stream.Dispose();
        }
      }
    }
  }
  catch(Exception e){
    Logger.WriteLine("Exception Caught ", e.ToString());
  }
}

The issue seems to be the Directory.GetFiles() function as it cannot access the images folder and is giving me the System.IO.IOException when it hits that line. But when I copy this exact path "\\192.168.97.1\images" to file explorer I can access the folder no problem.

I am writing any exceptions I get to another file through the Logger class so I know what's going on. This is the full error I am getting:

Exception Caught System.IO.IOException: The specified server cannot perform the requested operation

  at System.IO.Error.WinIOError(Int32 errorCode, String maybeFullPath)
  at System.IO.FileSystemEnumerableIterator`1.CommonInit()
  at System.IO.FileSystemEnumerableIterator`1..ctor(String path, String originalUserPath, String searchPattern, SearchOption searchOption, SearchResultHandler`1 resultHandler, Boolean checkHost)
  at System.IO.Directory.GetFiles(String path)
  at Project1.ServiceClass.TransferFiles()

I've tried playing with the format of the path itself, like giving it double back slashes as shown below, but its still giving me the same error

\\192.168.97.1\\images

Everyone has full permissions to this folder as well so I'm not sure what the issue is here. Any help on this would be greatly appreciated!

misterAl
  • 1
  • 1
  • I wouldn't think that the `Directory` class could perform SFTP commands. Why aren't you using the methods on the `sftp` variable to list and download files? – gunr2171 Apr 01 '22 at 17:35
  • Try `static string path = @"\\192.168.97.1\images";` Note the `@` before the string literal to treat it as written rather than interpret the backslashes as escape characters. Without that you'd need 4 backslashes at the beginning and two before images for a valid UNC path. – Retired Ninja Apr 01 '22 at 17:41
  • 1
    https://stackoverflow.com/questions/11009013/unc-path-does-not-work-with-net ? – user3953989 Apr 01 '22 at 18:16
  • Does this answer your question? [UNC path does not work with .NET?](https://stackoverflow.com/questions/11009013/unc-path-does-not-work-with-net) – Progman Apr 01 '22 at 20:29
  • Using @ before string path doesn't work. I've also tested \\\\192.168.97.1\\images path without the @ preceding it since I know that c# might interpret the two slashes as an escape sequence, but it still gives the same exception message. – misterAl Apr 06 '22 at 05:35
  • Also I should've mentioned this in my question (I'll edit it after), but the files I want to retrieve via network folder is actually on a linux machine but I can access it via file explorer on windows machine through that path, I don't know if this means that it requires linux path formatting? – misterAl Apr 06 '22 at 05:37

0 Answers0