我正在用C#编写一个HTML编辑器,其中包含FastColoredTextBox.dll组件。我有“另存为”选项的代码。但是当文件被保存时,我试着再次按下另存为选项,SaveFileDialog就会出现。我希望它保存到我们之前保存的文件中。这里是另存为代码:如何使用fastcoloredtextbox.dll组件保存HTML文件?
private void toolStripButton2_Click(object sender, EventArgs e)
{
SaveFileDialog sfd = default(SaveFileDialog);
if (FastColoredTextBox1.Text.Length > 0)
{
sfd = new SaveFileDialog();
sfd.Filter = "HTML Files|.html|" + "All Files|*.*";
sfd.DefaultExt = "html";
sfd.ShowDialog();
string location = null;
string sourcecode = FastColoredTextBox1.Text;
location = sfd.FileName;
if (!object.ReferenceEquals(sfd.FileName, ""))
{
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(location, false))
{
writer.Write(sourcecode);
writer.Dispose();
this.Text = "Netplait 2.5.1 - " + System.IO.Path.GetFullPath(location);
}
}
}
if (Directory.Exists(sfd.FileName) == true)
{
string location = sfd.InitialDirectory;
File.WriteAllText(location, (FastColoredTextBox1.Text));
}
}
请帮忙。
我不认为这有什么使用快速彩色文本框组件。您需要存储路径并不再次显示对话框。将路径存储在类变量中,并检查它是否在第一个if语句之前有值。如果确实如此,那么就保存到那条路上,否则就做你现在做的事。就目前而言,你没有逻辑来检查你是否曾经保存过一次。相反,你只是总是显示保存对话框。另外:不要使用ReferenceEquals检查字符串相等。使用string.Equals或==。由于它位于使用区块中,因此您无需在作家上调用Dispose。 – pinkfloydx33