If variable path
is empty, and editor.Text
is not empty, the SaveFileDialog should be displayed.
Now, why on earth is this damn thing failing???
I have tried this with many different variations of code with the same result: FAIL:
if(path.Length >= 1) // path contains a path. Save changes instead of creating NEW file.
{
File.WriteAllText(path, content);
}
else
{
// no path defined. Create new file and write to it.
using(SaveFileDialog saver = new SaveFileDialog())
{
if(saver.ShowDialog() == DialogButtons.OK)
{
File.WriteAllText(saver.Filename, content);
}
}
}
At the top of code file I have:
path = String.Empty;
So why the heck it this failing every single time, even after trying all of the below variations?
if(path.Length > 1) // path contains a path. Save changes instead of creating NEW file.
{
File.WriteAllText(path, content);
}
else
{
// no path defined. Create new file and write to it.
using(SaveFileDialog saver = new SaveFileDialog())
{
if(saver.ShowDialog() == DialogButtons.OK)
{
File.WriteAllText(saver.Filename, content);
}
}
}
and
if(String.IsNullOrEmpty(path)) // path contains a path. Save changes instead of creating NEW file.
{
File.WriteAllText(path, content);
}
else
{
// no path defined. Create new file and write to it.
using(SaveFileDialog saver = new SaveFileDialog())
{
if(saver.ShowDialog() == DialogButtons.OK)
{
File.WriteAllText(saver.Filename, content);
}
}
}
and
if(String.IsNullOrWhiteSpace(path)) // path contains a path. Save changes instead of creating NEW file.
{
File.WriteAllText(path, content);
}
else
{
// no path defined. Create new file and write to it.
using(SaveFileDialog saver = new SaveFileDialog())
{
if(saver.ShowDialog() == DialogButtons.OK)
{
File.WriteAllText(saver.Filename, content);
}
}
}
This is making me very angry. How could this fail?
Setting a break point reveals that path
is definitely null
/ ""
.
Why you've written:
if(saver.ShowDialog() == DialogButtons.OK)
Instead of:
if(saver.ShowDialog() == DialogResult.OK)
If path
is null
, you're going to get an exception when trying to get path.Length
. To check for an empty path, use the String.IsNullOrWhiteSpace(path)
version. You also need a condition to check your second requirement.
if(!String.IsNullOrWhiteSpace(path)) // path contains a path. Save changes instead of creating NEW file.
{
File.WriteAllText(path, content);
}
else if (!String.IsNullorWhiteSpace(editor.Text))
{
// no path defined. Create new file and write to it.
using(SaveFileDialog saver = new SaveFileDialog())
{
if(saver.ShowDialog() == DialogResult.OK)
{
File.WriteAllText(saver.Filename, content);
}
}
}
the path is a string and its the fullpath to your file? if its filled then it doesnt mean that the file really exists, you better go that way:
if(System.IO.File.Exists(path))
{
}
else
{
}
File.Exists(null) returns false, so this will work fine
if you wanna use your way, then i guess your last two statements are just missing a "!"
if (!String.IsNullOrWhiteSpace(path))
if(!String.IsNullOrEmpty(path))
check if null before access the length property
if(path != null && path.Length > 1)
Try this:
if (string.IsNullOrWhiteSpace(path) && !string.IsNullOrWhiteSpace(editor.Text))
{
// no path defined. Create new file and write to it.
using (SaveFileDialog saver = new SaveFileDialog())
{
if (saver.ShowDialog() == DialogResult.OK)
{
File.WriteAllText(saver.Filename, content);
}
}
}
else if(File.Exists(path)
{
File.WriteAllText(path, content);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.