如果變量path
爲空,並且editor.Text
不爲空,則應顯示SaveFileDialog。這個IF語句爲什麼失敗?
現在,爲什麼地球上這個該死的東西失敗?
我與的代碼具有相同的結果許多不同的變化嘗試這樣做: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
/""
。
只是要清楚:當你說「不及格」,你的意思是路徑爲空或空,但正在執行else分支? –
你還提到'和editor.Text'。你確定這不是導致if失敗的編輯嗎? – Tim
你的失敗是什麼意思? else語句不執行? – TrueEddie