C# webclient 下载报错
stringURLAddress=@"http://translate.google.cn/translate_tts?ie=UTF-8&q=你好&tl=zh-CN&to...
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();
client.DownloadFile(URLAddress, receivePath + System.IO.Path.GetFileName(URLAddress));
代码如上
input 结尾报错
如果url 换成 txt rar 等结尾都可以下载. 展开
string receivePath = @"C:\";
System.Net.WebClient client = new System.Net.WebClient();
client.DownloadFile(URLAddress, receivePath + System.IO.Path.GetFileName(URLAddress));
代码如上
input 结尾报错
如果url 换成 txt rar 等结尾都可以下载. 展开
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));
追问
并没有解决还是报错
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询