簡體   English   中英

如何使用fastcoloredtextbox.dll組件保存HTML文件?

[英]How to save an HTML file with fastcoloredtextbox.dll component?

我正在使用FastColoredTextBox.dll組件在C#中開發HTML編輯器。 我有“另存為”選項的代碼。 但是,當文件被保存時,我再次嘗試按另存為選項時,將出現SaveFileDialog。 我希望它保存到我們之前保存的文件中。 這是另存為代碼:

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

請幫忙。

我必須說,這段代碼對我來說似乎有些混亂。

為什么不創建對話框呢?

SaveFileDialog sfd = default(SaveFileDialog);

而且,為什么不檢查sfd.FileName != ""或更好的!string.IsNullOrWhitespace(sfd.FileName)

if (!object.ReferenceEquals(sfd.FileName, ""))

我是否誤解為sfd.FileName應該返回文件名,而不是目錄? 因為這樣,這段代碼沒有意義:

 if (Directory.Exists(sfd.FileName) == true)

無論如何,在打開對話框之前,您可以根據文檔設置sfd.InitialDirectorysfd.FileName

SaveFileDialog

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM