I am using c# .net windows form application. i have to save few inputs in defaultsetting.xml file but if there is invalid file with same file name "defaultsetting.xml" i should show msg in the status bar.. How can I do this?
5 Answers
Ask yourself: does the user need to know that the file failed to save?
If no, then handle the situation for them by overwriting the file. It will create a better experience as there less user interface friction/spam.
Example A
if (File.Exists(path))
File.Delete(path);
Save("defaultsettings.xml");
If yes, then check if the file exists and notify the user by either displaying a MessageBox
or changing the text label on your applications StatusStrip
.
Example B
if (File.Exists(path))
this.m_StatusBarLabel.Text = "Error: Could not write to file: \"" + path + "\"";
else
Save("defaultsettings.xml");
Where m_StatusBarLabel
is a ToolStripStatusLabel
that you added to your status strip control. Use the designer in Visual Studio to create this together (it is dead simple).
TIP: If the user needs to perform some action, make the text a HyperLink or add a Click event.
HTH,

- 20,275
- 4
- 64
- 80
-
Stop it with the calls to `File.ExistedInTheRecentPast` already. http://blogs.msdn.com/b/jaredpar/archive/2009/12/10/the-file-system-is-unpredictable.aspx – Ben Voigt Jun 10 '10 at 01:09
-
@BenVoight. Thanks for the link Ben. I agree with you that the file system should be treated as a dynamic shared resource, as more than likely you are not the only one accessing it. Whilst knowing this, the focus of this question and my answer was how/when to notifying the *user* of the situation. – Dennis Jun 10 '10 at 01:54
DO NOT USE File.Exists
!
Never use File.Exists
, it always introduces a race condition.
Instead, open the file in write-mode with the "creation only" option, handle the exception if the file already exists (as well as other errors, such as no permission to write in that directory, network share disconnected, etc.)

- 277,958
- 43
- 419
- 720
-
1
-
1http://blogs.msdn.com/b/jaredpar/archive/2009/12/10/the-file-system-is-unpredictable.aspx http://blogs.msdn.com/b/ericlippert/archive/2008/09/10/vexing-exceptions.aspx http://stackoverflow.com/questions/265953/how-can-you-easily-check-if-access-is-denied-for-a-file-in-net/265958#265958 – Ben Voigt Jun 10 '10 at 01:04
-
I agree that you have to be careful about using `File.Exists` but to say never use it is overreacting. I've used it quite a bit lately, in production systems, since it fits my needs exactly (I need to know if files have appeared, if they later disappear before I've finished processing, that would be a system failure which is handled as normally with normal Exception handling). – Hans Olsson Jun 10 '10 at 07:53
You can check if a file exists using File.Exists(path) and then display your message.

- 1,985
- 12
- 19
Did you mean StatusStrip?
Simply add a ToolStripStatusLabel to you StatusStrip and set the Text property of the label.
To check if the file exists use System.IO.File.Exists(filepath).

- 11,464
- 5
- 45
- 87
if (System.IO.File.Exists(@"C:\defaultsettings.xml"))
{
statusbar1.Text = "Default Settings already exists";
}
alternatively you could use this:
StreamWriter sw = null;
try
{
sw = new StreamWriter((Stream)File.Open(@"C:\DefaultSettings.txt", FileMode.CreateNew));
sw.WriteLine("Test");
}
catch (IOException ex)
{
if (ex.Message.Contains("already exists"))
{
statusbar1.Text = "File already exists";
}
else
{
MessageBox.Show(ex.ToString());
}
}
finally
{
if (sw != null)
{ sw.Close(); }
}

- 42,678
- 13
- 95
- 110