John Riker

John Riker

  • NA
  • 85
  • 14.3k

GetInvalidFileNameChars or part of the loop erroring

Nov 7 2020 8:47 AM
I have a variable with a path to a file I want to save and want to strip out any invalid characters that would cause the save or later move to fail.  It worked fine for a batch but now failing again.  I was going to remove the characters but for now just put a space in there.  So here's an example:
 
  1. foreach (char c in System.IO.Path.GetInvalidFileNameChars())  
  2.    {  
  3.       item.Snippet.Title = item.Snippet.Title.Remove(c, ' ');  
  4.    }  
 So it's erroring on line 3 above wit error:
 
Unhandled Exception: System.ArgumentOutOfRangeException: Index and count must refer to a location within the string.
Parameter name: count
at System.String.Remove(Int32 startIndex, Int32 count)
at ShowWebsiteDownloader.Program.Main() in C:\Users\syarbrou.ICEO\source\repos\ShowWebsiteDownloader\ShowWebsiteDownloader\Program.cs:line 278
 

At the start of that loop item.Snippet.Title contains:
 
Disney Fort Wilderness Halloween Staycation Check In Day! | Cabin Tour, Holiday Decorations & BBQ!
 
Thoughts what's going on? 

Answers (4)