File gets saved twice on local harddrive

Joe Green 146 Reputation points
2022-09-08T17:43:07.087+00:00

In .Net 6 using c#, I'm saving a test.pdf file to users temp folder (C:\Users\username\AppData\Local\Temp) but when I inspect the temp folder, I see file is saved twice one with a name upload_8d6371d032cfee57008b853cd3c87ae9.pdf and another copy with name test.pdf. I'm not sure why the file is being written twice and if I can get rid of this file upload_8d6371d032cfee57008b853cd3c87ae9.pdf or avoid file being written twice.

Here is my code

                // Full path to users local temp file folder, file.Name is test.pdf  
                string myTempFile = Path.Combine(Path.GetTempPath(), file.Name);  
  
                var httpClient = new HttpClient();  
  
                using (Stream stream = await httpClient.GetStreamAsync(remoteFileUrl).ConfigureAwait(false))  
                {  
                    using FileStream fileStream = new(myTempFile, FileMode.Create);  
                    stream.CopyTo(fileStream);  
                }  
  
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,321 questions
{count} votes