Check for valid file names

C

Chris

Hi,

In C# I tried to save a file from a generated file name.
Just before launching the dialog I check for a valid file
name to be sure.

There for I used the method ValidateNames from the save
dialog.

The strange thing is that sometimes the save dialogue
crashes on invalid file names (containing chars like ] ),
but the method ValidateNames return true.

Code looks like:
saveFileDialog = new System.Windows.Forms.SaveFileDialog();

saveFileDialog.FileName = (...) <--- generated file name
may contan invalid chars

if (!saveFileDialog.ValidateNames) <-- method returns true
for invalid file names ??

{

saveFileDialog.FileName =
"MyChart."+saveFileDialog.DefaultExt; <-- if name is
invalid, supply valid file name as default

}

if (saveFileDialog.ShowDialog() == DialogResult.OK)

{

}



Any idea what may be wrong ?

Thanks.

Bye

Chris
 
D

Dennis Myrén

ValidateNames is a property, not a method.
You need to initially set this to true, then the names will be validated.

By the way, the character ] is a valid character in a filename.
 
C

Chris

Thanks .. you are right.

But if I set that property ValidateNames on true, and so C#
wil check for valid file names, how can I check a priori if
my file name is valid. In order to replace it with a valid
default file name.

In C# there must be a simple method for that I guess ...

Some help woud be nice ...

Bye,

Chris
Dennis Myrén said:
ValidateNames is a property, not a method.
You need to initially set this to true, then the names will be validated.

By the way, the character ] is a valid character in a filename.



--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Chris said:
Hi,

In C# I tried to save a file from a generated file name.
Just before launching the dialog I check for a valid file
name to be sure.

There for I used the method ValidateNames from the save
dialog.

The strange thing is that sometimes the save dialogue
crashes on invalid file names (containing chars like ] ),
but the method ValidateNames return true.

Code looks like:
saveFileDialog = new System.Windows.Forms.SaveFileDialog();

saveFileDialog.FileName = (...) <--- generated file name
may contan invalid chars

if (!saveFileDialog.ValidateNames) <-- method returns true
for invalid file names ??

{

saveFileDialog.FileName =
"MyChart."+saveFileDialog.DefaultExt; <-- if name is
invalid, supply valid file name as default

}

if (saveFileDialog.ShowDialog() == DialogResult.OK)

{

}



Any idea what may be wrong ?

Thanks.

Bye

Chris
 
D

Dennis Myrén

I do not know of any particular feature that does that.
You would have to write your own function to test your filename.

--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Chris said:
Thanks .. you are right.

But if I set that property ValidateNames on true, and so C#
wil check for valid file names, how can I check a priori if
my file name is valid. In order to replace it with a valid
default file name.

In C# there must be a simple method for that I guess ...

Some help woud be nice ...

Bye,

Chris
Dennis Myrén said:
ValidateNames is a property, not a method.
You need to initially set this to true, then the names will be validated.

By the way, the character ] is a valid character in a filename.



--
Regards,
Dennis JD Myrén
Oslo Kodebureau
Chris said:
Hi,

In C# I tried to save a file from a generated file name.
Just before launching the dialog I check for a valid file
name to be sure.

There for I used the method ValidateNames from the save
dialog.

The strange thing is that sometimes the save dialogue
crashes on invalid file names (containing chars like ] ),
but the method ValidateNames return true.

Code looks like:
saveFileDialog = new System.Windows.Forms.SaveFileDialog();

saveFileDialog.FileName = (...) <--- generated file name
may contan invalid chars

if (!saveFileDialog.ValidateNames) <-- method returns true
for invalid file names ??

{

saveFileDialog.FileName =
"MyChart."+saveFileDialog.DefaultExt; <-- if name is
invalid, supply valid file name as default

}

if (saveFileDialog.ShowDialog() == DialogResult.OK)

{

}



Any idea what may be wrong ?

Thanks.

Bye

Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top