tags:

views:

45

answers:

3

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

+1  A: 

The SaveFileDialog class has a property called ValidateNames. It the value of that property is true(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: alt text

(yes, I am currently using Windows XP)

Fredrik Mörk
Can i have clear answer please
Dorababu
Ok. But how can i display the message of my own instead of default one.
Dorababu
@Dorababu: the code in your sample should probably work, if you set `ValidateNames` to `false` before displaying the dialog.
Fredrik Mörk
Even though i am unable to display the mesage box i required
Dorababu
+1  A: 

Set the property ValidateNames to true in the saveFileDialog1 instance as per this MSDN. And that is set to default to be true at run-time instantiation of the 'SaveFileDialog' class.

If you're talking about having a customized error message handler to display a custom message, you need to override the SaveFileDialog by sub-classing it and intercepting the windows procedure messages for this class. Have a look at this article on CodeProject which shows how to do it.

tommieb75
+1  A: 

This is because the FileDialog does this check already on himself.

If you try to use a < or a > within a filename you get an error message. If you try to use a search pattern like ? or * the ListView will be filtered for the given pattern.

Oliver