TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
C# Corner
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Remove Special Characters from File in C#
Mukesh Kumar
Oct 07
2015
Code
17.9
k
0
0
facebook
twitter
linkedIn
Reddit
WhatsApp
Email
Bookmark
expand
sing System;
using
System.Linq;
using
System.Text.RegularExpressions;
public
class
Test {
public
static
void
Main() {
var file_name = GetValidFileName(
"this is)file<ame.txt"
);
Console.WriteLine(GetValidFileName(file_name));
}
private
static
string
GetValidFileName(
string
fileName) {
// remove any invalid character from the filename.
String ret = Regex.Replace(fileName.Trim(),
"[^A-Za-z0-9_. ]+"
,
""
)
return
ret.Replace(
" "
, String.Empty);
}
}
C#
Remove Special Characters