0

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

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

路径 = 字符串。空;

那么,即使在尝试了以下所有变体之后,为什么每次都失败呢?

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

4 回答 4

5

你为什么写:

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

代替:

if(saver.ShowDialog() == DialogResult.OK)
于 2013-07-03T16:59:52.803 回答
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);
      }
   }
}
于 2013-07-03T17:12:15.983 回答
1

路径是一个字符串,它是文件的完整路径?如果它已满,则并不意味着该文件确实存在,您最好这样做:

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) 
于 2013-07-03T17:00:41.313 回答
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);
  }
于 2013-07-03T17:15:39.417 回答