variablepath
が空で、editor.Text
空でない場合、SaveFileDialog が表示されます。
さて、一体なぜこのいまいましいことは失敗しているのですか?
私は同じ結果でコードの多くの異なるバリエーションでこれを試しました: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);
}
}
}
私が持っているコードファイルの上部に:
パス = String.Empty;
では、以下のバリエーションをすべて試した後でも、毎回これが失敗するのはなぜですか?
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);
}
}
}
と
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);
}
}
}
と
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);
}
}
}
これは私をとても怒らせています。これはどのように失敗する可能性がありますか?
path
ブレークポイントを設定すると、それが間違いなく null
/であることがわかり""
ます。