Click here to Skip to main content
15,909,242 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear,

C# webclient, how to solve the problem about the download file name contain \ / : * ? " < > this symbols?

Best Regards.
Posted

Try using @ or \ escape sequences when you are defining the file name.
 
Share this answer
 
Comments
tfyuhijk 16-May-11 3:14am    
Sorry, i don't really get it....because i'm using visual studio 2010, is it possible for you to give me some reference website? Thanks you so much
Sergey Alexandrovich Kryukov 16-May-11 3:18am    
There is no website for this! Look at C# syntax and learn how to write string literals. Look just at the standard or any manual (MSDN, for example).

The problem itself is different. Please see my answer.
Sergey Alexandrovich Kryukov 16-May-11 3:19am    
Abhinav, the answer is correct (a 5) but maybe not quite complete.
Please see my answer.
--SA
Abhinav S 16-May-11 3:21am    
Thank you SA.
A file name hardly can contain ":", "*" or "?". From the client standpoint, this is not a file but URL, so the directory delimiter is "/" (however "\" is also accepted if the URL is a local file). How can this be a problem?

Abhinav correctly explained that you can put any of these characters in a string literal. Is this was a problem? If it was, remember that there is practically no situation when you should use hard-coded string constants in your code, especially it this is a URL.

—SA
 
Share this answer
 
Comments
Abhinav S 16-May-11 3:20am    
Complete answer. My 5.
Sergey Alexandrovich Kryukov 16-May-11 4:24am    
Thank you, Abhinav.
--SA
tfyuhijk 16-May-11 3:24am    
client.DownloadFile(item.Link, @"C:\Jobresult - " + item.Title + ".html");

visual studio said An exception occurred during a WebClient request. so i was wondering is it something wrong about the symbols
Sergey Alexandrovich Kryukov 16-May-11 4:28am    
Are you sure the exception is in this line? Did you do it under debugger or saw exception stack?
Also, repeating "+" is evil (because string is immutable, performance problem). Correct way it:

string url = string.Format(@"file://C:\Jobresult - {0}.html", item.Title).

But - don't hard-code format string, put it in resource where you can write it verbosely.
Also, pay attention -- I added file:// schema to the URL.
--SA
Check out Uri.EscapeUriString[^].
Use the EscapeUriString method to prepare an unescaped URI string to be a parameter to the Uri constructor.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900