22using System . Collections . Generic ;
33using System . IO ;
44using System . Linq ;
5+ using System . Text . RegularExpressions ;
56using YoutubeExtractor ;
67
78namespace ExampleApplication
@@ -25,7 +26,8 @@ private static void DownloadAudio(IEnumerable<VideoInfo> videoInfos)
2526 */
2627
2728 var audioDownloader = new AudioDownloader ( video ,
28- Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . MyDocuments ) , video . Title + video . AudioExtension ) ) ;
29+ Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . MyDocuments ) ,
30+ RemoveIllegalPathCharacters ( video . Title ) + video . AudioExtension ) ) ;
2931
3032 // Register the progress events. We treat the download progress as 85% of the progress
3133 // and the extraction progress only as 15% of the progress, because the download will
@@ -54,7 +56,8 @@ private static void DownloadVideo(IEnumerable<VideoInfo> videoInfos)
5456 * The second argument is the path to save the video file.
5557 */
5658 var videoDownloader = new VideoDownloader ( video ,
57- Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . MyDocuments ) , video . Title + video . VideoExtension ) ) ;
59+ Path . Combine ( Environment . GetFolderPath ( Environment . SpecialFolder . MyDocuments ) ,
60+ RemoveIllegalPathCharacters ( video . Title ) + video . VideoExtension ) ) ;
5861
5962 // Register the ProgressChanged event and print the current progress
6063 videoDownloader . DownloadProgressChanged += ( sender , args ) => Console . WriteLine ( args . ProgressPercentage ) ;
@@ -80,5 +83,12 @@ private static void Main()
8083 //DownloadAudio(videoInfos);
8184 DownloadVideo ( videoInfos ) ;
8285 }
86+
87+ private static string RemoveIllegalPathCharacters ( string path )
88+ {
89+ string regexSearch = new string ( Path . GetInvalidFileNameChars ( ) ) + new string ( Path . GetInvalidPathChars ( ) ) ;
90+ var r = new Regex ( string . Format ( "[{0}]" , Regex . Escape ( regexSearch ) ) ) ;
91+ return r . Replace ( path , "" ) ;
92+ }
8393 }
8494}
0 commit comments