2013-07-03 174 views
0

如果變量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/""

+2

只是要清楚:當你說「不及格」,你的意思是路徑爲空或空,但正在執行else分支? –

+0

你還提到'和editor.Text'。你確定這不是導致if失敗的編輯嗎? – Tim

+1

你的失敗是什麼意思? else語句不執行? – TrueEddie

回答

5

爲什麼你寫的:

if(saver.ShowDialog() == DialogButtons.OK) 

相反的:

if(saver.ShowDialog() == DialogResult.OK) 
+1

哇,好抓! –

+0

是的Visual Studio沒有響應,所以我不能複製/粘貼,所以我只是決定寫在這裏。我感到困惑:) –

1

的路徑是一個字符串,其FULLPATH到您的文件?如果它然後填充它不意味着該文件確實存在,你最好走那條路:

if(System.IO.File.Exists(path)) 
{ 

} 
else 
{ 

} 

File.Exists(空)返回false,所以這將很好地工作

,如果你想用你的方式,那麼我想你最後的兩個陳述只是缺少一個「!」

if (!String.IsNullOrWhiteSpace(path)) 

if(!String.IsNullOrEmpty(path)) 

檢查,如果訪問之前空length屬性

if(path != null && path.Length > 1) 
+0

我不想檢查文件是否存在。我也知道如何做到這一點。但是,謝謝。 –

+0

歡迎您,查看我的編輯。我猜你剛纔錯過了一個沒有問之前是否空着 – Koryu

2

如果pathnull,你會得到一個異常試圖讓path.Length時。要檢查空路徑,請使用String.IsNullOrWhiteSpace(path)版本。你還需要一個條件來檢查你的第二個要求。

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); 
     } 
    } 
} 
0

試試這個:

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); 
    }