Hi all i have written a code to display a message box if invalid characters are entered while saving the file but my message box is not displaying. Actually i will have a save file dialog option to save a file if the filename starts or consists of the following
\\/:*?<>|"
I would like to display a message box as invalid or illegalcharacters in file
My code is as follows
Stream myStream;
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
saveFileDialog1.InitialDirectory = @"C:\";
saveFileDialog1.DefaultExt = "txt";
saveFileDialog1.Filter = "(*.txt)|*.txt";
saveFileDialog1.FilterIndex = 2;
saveFileDialog1.RestoreDirectory = true;
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
{
FileName = saveFileDialog1.FileName;
if ((FilePathHasInvalidChars(FileName)))
{
MessageBox.Show("File name should not contain \\/:*?<>|" ,"", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
if ((myStream = saveFileDialog1.OpenFile()) != null)
{
//FileName = saveFileDialog1.FileName;
if (!(FilePathHasInvalidChars(FileName)))
{
TreeNode newNode = new TreeNode(FileName);
newNode.SelectedImageIndex = 1;
tvwACH.SelectedNode.Nodes.Add(newNode);
TreeNode NodeFileHeader = newNode.Nodes.Add("FileHeader");
myStream.Close();
}
}
}
}
public static bool FilePathHasInvalidChars(string path)
{
return (!string.IsNullOrEmpty(path) && path.IndexOfAny(System.IO.Path.GetInvalidPathChars()) >= 0);
}
Can any one help me
The

SaveFileDialogclass has a property calledValidateNames. It the value of that property istrue(which it is by default, no need to assign it), the dialog will automatically validate that the name the the user enters does not contain any illegal characters. If the user enters an illegal file name and clicks the “Save” button, the dialog will not close, but instead show an error message:(yes, I am currently using Windows XP)