so I have the following code which generates a sha256 hash of the file data of the file that was uploaded by the user. This works fine but sometimes it includes illegal characters (for the windows os).
So what I'm trying to implement is a try catch to strip of illegal characters. I have pulled this information off of Microsoft's website itself. However, when implemented with Regex.Replace()
I'm told that it only accepts 5 overloads.
Which is confusing because that is what I have and I have triple checked that my hashedfile1name
is a string type variable.
The other problem is that for the try catch its telling me that it doesn't know what RegexMatchTimeoutException
is. But there are no more imports/using statements in Microsoft's example.
try
{
FileUpload1.SaveAs("C:\\direct\\uploads\\" + FileUpload1.FileName);
using (fs = File.OpenRead("C:\\direct\\uploads\\" + FileUpload1.FileName))
{
byte[] bytes = new byte[fs.Length];
fs.Read(bytes, 0, Convert.ToInt32(fs.Length));
hashedfile1name = Convert.ToBase64String(hash.ComputeHash(bytes));
}
try
{
Regex.Replace(hashedfile1name, @"[^\w\.@-]", "", RegexOptions.None, TimeSpan.FromSeconds(1.5));
}
catch (RegexMatchTimeoutException)
{
hashedfile1name = "";
}
FileUpload1.SaveAs("C:\\direct\\uploads\\" + hashedfile1name);
File.Delete("C:\\direct\\uploads\\" + FileUpload1.FileName);
Label1.Text = "File name: " + FileUpload1.PostedFile.FileName + " - " + hashedfile1name;
}
catch (Exception ex)
{
Label1.Text = "ERROR: " + ex.Message.ToString();
}