我正在用 C# 开发一个语法编辑器,您可以在其中编写FastColoredTextBox组件中的代码,然后将其保存为 .html 文件。但是,我有该Save As
选项的代码。我唯一的问题是当用户保存 .html 文件时,会Save As
弹出相同的对话框。但是我们之前已经保存了它。我只想按Ctrl+S
一下键盘,它会在保存为 .html 文件后自动保存文件更改。
这是我的Save As
选项代码。
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();
}
}
}
if (Directory.Exists(sfd.FileName) == true)
{
string location = sfd.InitialDirectory;
File.WriteAllText(location, (FastColoredTextBox1.Text));
}
}
谁能帮我实现这一目标?请帮忙。