为什么这个IF语句失败了?

时间:2013-07-03 16:49:28

标签: c# .net wpf winforms visual-studio

如果变量path为空,且editor.Text不为空,则应显示SaveFileDialog。

现在,为什么这个该死的东西失败了?

我尝试了许多不同的代码变体,结果相同:失败:

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

在代码文件的顶部,我有:

path = 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 / ""

4 个答案:

答案 0 :(得分:5)

你为什么写:

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

而不是:

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

答案 1 :(得分: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);
      }
   }
}

答案 2 :(得分:1)

路径是一个字符串,它是文件的完整路径?如果它被填充然后它并不意味着文件真的存在,你最好这样:

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

}
else
{

}

File.Exists(null)返回false,所以这样可以正常工作

如果你想用你的方式,那么我猜你的最后两个陈述只是缺少一个“!”

if (!String.IsNullOrWhiteSpace(path)) 

if(!String.IsNullOrEmpty(path))

在访问length属性

之前检查是否为null
if(path != null && path.Length > 1) 

答案 3 :(得分: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);
  }