views:

425

answers:

2

Hi, I've wrote this little method to achieve the goal in the subj., however, is there more efficient (simpler) way of doing this? I hope this can help somebody who will search for this like I did.

var fileName = new System.Text.StringBuilder();
fileName.Append("*Bad/\ :, Filename,? ");
// get rid of invalid chars
while (fileName.ToString().IndexOfAny(System.IO.Path.GetInvalidFileNameChars()) > -1)
{
    fileName = fileName.Remove(fileName.ToString().IndexOfAny(System.IO.Path.GetInvalidFileNameChars()), 1);
}

?

+4  A: 

Try the following

public string MakeValidFileName(string name) {
  var builder = new StringBuilder();
  var invalid = System.IO.Path.GetInvalidFileNameChars();
  foreach ( var cur in name ) {
    if ( !invalid.Contains(cur) ) {
      builder.Append(cur);
    }
  }
  return builder.ToString();
}
JaredPar
+1  A: 

If you look for "concise" when you say simple:

public string StripInvalidChars(string filename) {
  return new String(
    filename.Except(System.IO.Path.GetInvalidFileNameChars()).ToArray()
  );
}

That said, I'd go with JaredPar's solution. It's probably easier to read (depending on taste, background), my gut feeling is that it is more efficient (although I'm not sure how efficient you have to be stripping that dozen of invalid chars from a limited length filename) and his use of a StringBuilder() seems to fit perfectly to your example.

Benjamin Podszun
I don't believe the Except method does what you were wanting here. It doesn't really work with duplicates. "[The Except] method returns those elements in first that do not appear in second. It does not also return those elements in second that do not appear in first." via http://msdn.microsoft.com/en-us/library/bb300779.aspx
ChronoPositron
@ChronoPositron: Enlighten me: What is the the problem? I want "those elements in first that do not appear in second" (i.e. the chars that are not invalid). I don't want "those elements in second that do not appear in first" (i.e. the invalid chars that are not present).
Benjamin Podszun
@Bejamin Podszun: The issue is that Except works as a set operation. For example, if I pass in "aaabbb.txt" (which is a valid filename) to your function, the resulting value is "ab.tx". It is only keeping the first occurrence of each letter, which makes it remove a lot more than just the invalid characters; it changes the expected outcome of the function.
ChronoPositron
@ChronoPositron: Whoa, you got me now. I was missing the "set" part before and when I quickly tested this in LINQPad with the testdata of the OP I merely noticed the removed invalid chars (good) and not the missing valid elements (bad). Sorry for being slow and thanks for enlightening me.
Benjamin Podszun