Home > Software design >  .net core 3.1 WebClient upload Could not find a part of the path error
.net core 3.1 WebClient upload Could not find a part of the path error

Time:12-31

I am trying to upload a file using the below code. But I am getting the error message Could not find a part of the path 'C:\source\repos\FTPTest\FTPTest\bin\Debug\netcoreapp3.1\ftp3.test.com\test123.txt'

static void ftpuploadtest()
        {
            String uriString = "ftp3.test.com/test123.txt";
            WebClient myWebClient = new WebClient();
            myWebClient.Credentials = new NetworkCredential("username", "password");
            string fileName = "C:\\Temp\\unitssstest.txt"; 
            byte[] responseArray = myWebClient.UploadFile(uriString, fileName);
            Console.WriteLine("\nResponse Received. The contents of the file uploaded are:\n{0}",
                System.Text.Encoding.ASCII.GetString(responseArray));
            
        }

Error

System.Net.WebException
  HResult=0x80070003
  Message=Could not find a part of the path 'C:\source\repos\FTPTest\FTPTest\bin\Debug\netcoreapp3.1\ftp3.test.com\test123.txt'.
  Source=System.Net.Requests
  StackTrace:
   at System.Net.FileWebRequest.CreateWriteStream()
   at System.Net.FileWebRequest.<>c.<BeginGetRequestStream>b__55_0(Object s)
   at System.Threading.Tasks.Task`1.InnerInvoke()
   at System.Threading.Tasks.Task.<>c.<.cctor>b__274_0(Object obj)
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
--- End of stack trace from previous location ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread)
--- End of stack trace from previous location ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at System.Threading.Tasks.TaskToApm.End[TResult](IAsyncResult asyncResult)
   at System.Net.FileWebRequest.EndGetRequestStream(IAsyncResult asyncResult)
   at System.Net.FileWebRequest.GetRequestStream()
   at System.Net.WebClient.UploadBits(WebRequest request, Stream readStream, Byte[] buffer, Int32 chunkSize, Byte[] header, Byte[] footer)
   at System.Net.WebClient.UploadFile(Uri address, String method, String fileName)
   at System.Net.WebClient.UploadFile(String address, String fileName)
   at FTPTest.Program.ftpuploadtest() in C:\source\repos\FTPTest\FTPTest\Program.cs:line 20
   at FTPTest.Program.Main(String[] args) in C:\source\repos\FTPTest\FTPTest\Program.cs:line 11

  This exception was originally thrown at this call stack:
    [External Code]

Inner Exception 1:
DirectoryNotFoundException: Could not find a part of the path 'C:\source\repos\FTPTest\FTPTest\bin\Debug\netcoreapp3.1\ftp3.test.com\test123.txt'.

CodePudding user response:

Your URI string is being treated as relative and combined with the default BaseAddress (see Remarks on UploadFile)

Changing it to an absolute URI should resolve it (likely ftp://ftp3.test.com/test123.txt)

  • Related