C# webclient 下载报错

2024-12-04 03:13:12
推荐回答(1个)
回答1:

报错的是 因为System.IO.Path.GetFileName(URLAddress) 返回的字符串 带有非法字符 ‘?’

string URLAddress = @"http://translate.google.cn/translate_tts?ie=UTF-8&q=你好尘旅&乎兄瞎tl=zh-CN&total=1&idx=0&textlen=9&prev=input"; 
            string receivePath = @"C:\";
            System.Net.WebClient client = new System.Net.WebClient();
string p=System.IO.Path.GetFileName(URLAddress);
            foreach (var element in Path.GetInvalidFileNameChars()) {
              岁空  p=p.Replace(element,'_');
            }
client.DownloadFile(URLAddress, receivePath + p));