jay*_*t55 0 .net c# wpf visual-studio winforms
如果变量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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
在代码文件的顶部我有:
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
和
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
和
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);
}
}
}
Run Code Online (Sandbox Code Playgroud)
这让我非常生气.怎么会失败?
设置断点显示肯定path是/ . null""
你为什么写:
if(saver.ShowDialog() == DialogButtons.OK)
Run Code Online (Sandbox Code Playgroud)
代替:
if(saver.ShowDialog() == DialogResult.OK)
Run Code Online (Sandbox Code Playgroud)