为什么这个IF语句失败

本文关键字:语句 失败 IF 为什么 | 更新日期: 2023-09-27 18:20:57

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

为什么这个IF语句失败

为什么要写:

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

代替:

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

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

路径是字符串,它是文件的完整路径?如果它被填满了,那么这并不意味着文件真的存在,你最好这样做:

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

File.Exists(null)返回false,因此这将正常工作

如果你想用你的方式,那么我猜你的最后两个语句只是缺少一个"!"

if (!String.IsNullOrWhiteSpace(path)) 
if(!String.IsNullOrEmpty(path))

在访问长度属性之前检查是否为null

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

试试这个:

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