none
C# 从bing下载图片出现错误 RRS feed

  • Soru

  • public void Saving(string fileName,string url)       
     {            
          using (WebClient client = new WebClient())           
          {               
              client.DownloadFile(new Uri(url), fileName);           
          }      
     }
    
    private void wallpaper_Click(object sender, RoutedEventArgs e)        
    {            
              m_fileName = m_namenumber + ".png";            
              try            
              {                
                    saveImage.Saving(m_fileName, BingImage.GetBingImageUrl(url));           
              }           
              catch (ExternalException ed)           
              {                
                     Console.WriteLine(ed.Message);           
              }           
             catch (ArgumentNullException ed)            
              {                
                     Console.WriteLine(ed.Message);           
               }       
     }

    在这一句出现client.DownloadFile(new Uri(url), fileName);错误

    用户未处理的异常

    System.Net.WebException:“An exception occurred during a WebClient request.”

    内部处理异常

    UnauthorizedAccessException: Access to the path 'E:\VS2019\project\2022.12.09\bing壁纸开发\App_bing2\bin\x64\Debug\AppX\0.png' is denied.

    此异常最初是在此调用堆栈中引发的: 
        [外部代码]

    请问这是因为什么呢?

    12 Aralık 2022 Pazartesi 10:02

Yanıtlar

  • 你好,笑先生:

      这个错误是由于您在尝试将文件保存到磁盘时没有足够的权限导致的。

      有几种可能的原因导致这个错误。

      例如,您可能没有足够的权限访问该路径,或者该路径可能不存在。

      您可能需要确保您有足够的权限访问该路径,或者您可能需要更改保存文件的路径。

      尝试以下保存到桌面的代码是否起作用。

    string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
    string fileName = Path.Combine(desktopPath, m_namenumber + ".png");
    saveImage.Saving(fileName, BingImage.GetBingImageUrl(url));
      你需要确保目标地址的存在,并且确保当前用户拥有足够的权限。

      你可以尝试右键使用管理员打开应用。

    Best Regards,

    Jiale


    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 MSDNFSF@microsoft.com.

    • Yanıt Olarak İşaretleyen 笑先生 5 Ocak 2023 Perşembe 02:45
    23 Aralık 2022 Cuma 09:56