Hi georgeacuster...,
You need to first find filename without extension using GetFileNameWithoutExtension method.
After that find file extension using GetExtension method.
Then use RegularExpression to replace special character Regex.Replace and add the extension.
Please refer below sample.
HTML
<asp:FileUpload ID="fuDetails" runat="server" />
<asp:Button Text="Upload" OnClick="Upload" runat="server" />
<br />
<asp:Label ID="lblFileName" runat="server" />
Namespaces
C#
using System.IO;
using System.Text.RegularExpressions;
VB.Net
Imports System.IO
Imports System.Text.RegularExpressions
Code
C#
protected void Upload(object sender, EventArgs e)
{
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fuDetails.PostedFile.FileName);
string fileExtension = Path.GetExtension(fuDetails.PostedFile.FileName);
string fileNameCheckName = Regex.Replace(fileNameWithoutExtension.ToUpper(), @"[^\w]", "_") + fileExtension;
lblFileName.Text = fileNameCheckName;
}
VB.Net
Protected Sub Upload(ByVal sender As Object, ByVal e As EventArgs)
Dim fileNameWithoutExtension As String = Path.GetFileNameWithoutExtension(fuDetails.PostedFile.FileName)
Dim fileExtension As String = Path.GetExtension(fuDetails.PostedFile.FileName)
Dim fileNameCheckName As String = Regex.Replace(fileNameWithoutExtension.ToUpper(), "[^\w]", "_") & fileExtension
lblFileName.Text = fileNameCheckName
End Sub
Screenshot