Share via


c# how delete webClient.DownloadFile ?

Question

Thursday, February 2, 2017 9:50 AM

I'm using Webclient.DownloadFile() in C# to save some files. If the file already exists i want to overwrite it. Does this method overwrite a file if it exists?? It Doesn't seem to be overwriting it so far.

My code:

try
{
      teks = valueCell;
      mp3Path = Environment.CurrentDirectory + @"tmp.mp3";
      urltts = new Uri("http://translate.google.com/translate_tts?client=tw-ob&tl=de&q=" + teks);

      if (File.Exists(mp3Path))
      {                                               
           File.Delete(mp3Path);
      }
      else
      {                       
           using (tts = new WebClient())
           {                               

             tts.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/4.0 (compatible; MSIE 9.0; Windows;)");
             tts.DownloadFile(urltts, mp3Path);
             tts.Dispose();

           }
       }
                   
       mediaPlayer.URL = mp3Path;                     
}
catch (Exception ex)
{
      MessageBox.Show("Error" + ex);
}
 teks = null;

All replies (5)

Thursday, February 2, 2017 10:02 AM

Hi marcionitao,

The reason file not getting deleted because the file path creating wrong in the concatenation. A slash was missing before file name. Please add a slash before your file name and see below. Hope this helps you.

"c:\users\username\documents\visual studio 2013\Projects\FileDeleteTest\FileDeleteTest\bin\Debugtmp.mp3"

Modified code this way:

      teks = valueCell;
      mp3Path = Environment.CurrentDirectory + @"\tmp.mp3"; //Here a slash was missing
      

Now directory path output:

"c:\users\username\documents\visual studio 2013\Projects\FileDeleteTest\FileDeleteTest\bin\Debug\tmp.mp3"

Thanks,
Sabah Shariq

[If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


Thursday, February 2, 2017 10:29 AM

Thanks Sabah Shariq,

And when I try to rewrite:

if (File.Exists(mp3Path))
{                                                     
     File.Delete(mp3Path);

     using (tts = new WebClient())
     {                          
        tts.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/4.0 (compatible; MSIE 9.0; Windows;)");
        tts.DownloadFile(urltts, mp3Path);
      }

}
else
{
      using (tts = new WebClient())
      {                                  
         tts.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/4.0 (compatible; MSIE 9.0; Windows;)");
         tts.DownloadFile(urltts, mp3Path);
         tts.Dispose();
      }
}

I get this Error:


ErrorSystem.Net.WebException: Excepção durante um pedido WebClient. > System.UnauthorizedAccessException: Acesso negado ao caminho 'C:\Users\Márcio\documents\visual studio 2015\Projects\ubersetzer\ubersetzer\bin\Release\tmp.mp3'.
   em System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   em System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   em System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
   em System.Net.WebClient.DownloadFile(Uri address, String fileName)
    Fim do rastreio da pilha de excepção interna 
   em System.Net.WebClient.DownloadFile(Uri address, String fileName)
   em ubersetzer.Form1.übersetzerDataGridView_CellClick(Object sender, DataGridViewCellEventArgs e) em C:\Users\Márcio\documents\visual studio 2015\Projects\ubersetzer\ubersetzer\Form1.cs:line 259

OK   

Thursday, February 2, 2017 10:33 AM

That shows that the system is trying to overwrite the file but, exactly as the error says, it does not have access.

Your app may need to run as administrator, or if your app is a service (like a web service) it will be running under some other account that may not have access to your personal documents folder.


Saturday, February 18, 2017 6:28 AM

Hi marcionitao,

Welcome to MSDN forum.

For your error message, where do get it? 

If you use the following logic, the code works well.

if (File.Exists(mp3Path))
{                                                     
     File.Delete(mp3Path);

     using (tts = new WebClient())
     {                          
        tts.DownloadFile(urltts, mp3Path);
      }

}
else
{
      using (tts = new WebClient())
      {                                  
         tts.DownloadFile(urltts, mp3Path);
      }
}

What does the following code you want to do?

 tts.Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/4.0 (compatible; MSIE 9.0; Windows;)");
        

Best Regards,

Wendy

MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact [email protected].


Monday, February 27, 2017 2:51 PM

Hi marcionitao,

If your issue is solved please Mark as answer or Vote as helpful post to the appropriate answer so that it will help other members to find solution if they faces similar issue.

Your understanding and cooperation will be grateful.

Thanks,
Sabah Shariq

[If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]