Create safe File Name from string using .NET 2.0

Posted at: 5/19/2007 at 10:32 AM by saravana

Recently for one of my weekend project I wanted to create a safe file name from a string. In the past people used to do lot of regular expressions and conditional testing to meet this requirement. Still the filename won't be safe across multiple environments (Windows 2000/XP/2003 etc). But with .NET 2.0 using couple of inbuild functions (Path.GetInvalidFileNameChars and Path.GetInvalidPathChars) you can create safe filenames. Hope this piece of code will be useful to someone.

private string CreateValidFileName(string title, string extension)
{
string validFileName = title.Trim();

foreach (char invalChar in Path.GetInvalidFileNameChars())
{
validFileName = validFileName.Replace(invalChar.ToString(), "");
}
foreach (char invalChar in Path.GetInvalidPathChars())
{
validFileName = validFileName.Replace(invalChar.ToString(), "");
}

if (validFileName.Length > 160) //safe value threshold is 260
validFileName = validFileName.Remove(156) + "..."

return validFileName + "." + extension;
}

Nandri!

Saravana

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5
Tags:  Categories: .NET
Actions: Email this article Email | Kick it! | DZone it! | Save to del.icio.us | Technorati Links
Post Information: Permanent LinkPermalink | CommentsComments(3) | Comments RSS

Comments

Tuesday, May 22, 2007 5:25 PM
Larry R
Why do you do this :
if (validFileName.Length > 160) //safe value threshold is 260
validFileName = validFileName.Remove(156) + "..." return validFileName + "." + extension;
}
?
Tuesday, May 22, 2007 5:33 PM
Saravana Kumar
In my case I only need 160 characters as file name, so I remove everthing after 156 characters and append it with "...". Also, some systems based on the filesystem won't allow more than 256 char, so its safe to restrict the length.

Saravana
Sunday, April 27, 2008 5:36 AM
Tony
Tony
Hi, I came here to look for a solution. The above code was not necessarily what I was looking for. What I wanted to do was if I had a string such as 'desktop/newfolder/file.html' I would want to convert this string to 'file.html'.

I wrote my own code that worked for this problem. I just thought I would share with you all :

private string CreateValidFileName(string title)
{
// remove all white spaces
title = title.Trim();

string validFileName = title;

// loop through the length of the string
for (int i = 0; i < title.Length; i++)
{
// if the current string is a '/'
if(title[i] == '/')
{
// set the valid file to all characters after the '/'
validFileName = title.Substring(i, (title.Length - i));
}
}

// remove the front '/' ex. ( file name of '/file.swf' would be 'file.swf' )
validFileName = validFileName.Remove(0, 1);

// Return the Valid filename
return validFileName;
}






Just copy and paste the code and it will work. Hope this helps someone with the same issue Smile

Add comment


(Will show your Gravatar icon)  

  Country flag

biuquote
  • Comment
  • Preview
Loading