C# 如何制作“保存”按钮,其中“保存文件”对话框出现一次,如果再次单击,则更新保存的文件

C# 如何制作“保存”按钮,其中“保存文件”对话框出现一次,如果再次单击,则更新保存的文件,c#,save,savefiledialog,save-as,C#,Save,Savefiledialog,Save As,正如标题所说,我正在尝试创建一个保存按钮,它将在第一次单击用户名、文件等时提示SaveFileDialogue。当再次单击保存按钮时,文件将被重新保存更新,并且SFD不会再次提示 以下是我的保存按钮单击方法: private void btnSave_Click(object sender, EventArgs e) { if (SaveDoc.ShowDialog() == DialogResult.OK) //SaveDoc is a SaveFileDialogu

正如标题所说,我正在尝试创建一个保存按钮,它将在第一次单击用户名、文件等时提示SaveFileDialogue。当再次单击保存按钮时,文件将被重新保存更新,并且SFD不会再次提示

以下是我的保存按钮单击方法:

 private void btnSave_Click(object sender, EventArgs e)
    {
        if (SaveDoc.ShowDialog() == DialogResult.OK) //SaveDoc is a SaveFileDialogue placed on the designer.
        {
            CreateWordDocument(tFilename.Text, SaveDoc.FileName, pathImage);
            tEnabled(false);
        }
    }
这是完整的代码

 public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    //Method: Find and Replace:
    private void FindAndReplace(Microsoft.Office.Interop.Word.Application wordApp, object findText, object replaceWithText)
    {
        object matchCase = true;
        object matchWholeWord = true;
        object matchWildCards = false;
        object matchSoundLike = false;
        object nmatchAllForms = false;
        object forward = true;
        object format = false;
        object matchKashida = false;
        object matchDiactitics = false;
        object matchAlefHamza = false;
        object matchControl = false;
        object read_only = false;
        object visible = true;
        object replace = 2;
        object wrap = 1;

        wordApp.Selection.Find.Execute(ref findText,
                    ref matchCase, ref matchWholeWord,
                    ref matchWildCards, ref matchSoundLike,
                    ref nmatchAllForms, ref forward,
                    ref wrap, ref format, ref replaceWithText,
                    ref replace, ref matchKashida,
                    ref matchDiactitics, ref matchAlefHamza,
                    ref matchControl);
    }

    string pathImage = null;

    //Method: Create the document :
    private void CreateWordDocument(object filename, object saveAs , object image)
    {
        object missing = Missing.Value;
        string tempPath=null;

        Word.Application wordApp = new Word.Application();

        Word.Document aDoc = null;

        if (File.Exists((string)filename))
        {
            DateTime today = DateTime.Now;

            object readOnly = false; //default
            object isVisible = false;

            wordApp.Visible = false;

            aDoc = wordApp.Documents.Open(ref filename, ref missing, ref readOnly,
                                        ref missing, ref missing, ref missing,
                                        ref missing, ref missing, ref missing,
                                        ref missing, ref missing, ref missing,
                                        ref missing, ref missing, ref missing, ref missing);

            aDoc.Activate();

            //Find and replace:
            this.FindAndReplace(wordApp, "<name>", tFirstname.Text);
            this.FindAndReplace(wordApp, "<Lastname>", tLastname.Text);
            this.FindAndReplace(wordApp, "<tel>", tPhone.Text);
            this.FindAndReplace(wordApp, "<Company>", tCompany.Text);
            this.FindAndReplace(wordApp, "<Date>", DateTime.Now.ToShortDateString());

            //insert the picture:
            Image img = resizeImage(pathImage, new Size(200, 90));
            tempPath = System.Windows.Forms.Application.StartupPath + "\\Images\\~Temp\\temp.jpg";
            img.Save(tempPath);

            Object oMissed = aDoc.Paragraphs[1].Range; //the position you want to insert
            Object oLinkToFile = false;  //default
            Object oSaveWithDocument = true;//default
            aDoc.InlineShapes.AddPicture(tempPath, ref  oLinkToFile, ref  oSaveWithDocument, ref oMissed);

        }
        else
        {
            MessageBox.Show("file dose not exist.");
            return;
        }

        //Save as: filename
        aDoc.SaveAs2(ref saveAs, ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing,
                ref missing, ref missing, ref missing);

        //Close Document:
        aDoc.Close(ref missing, ref missing, ref missing);
        File.Delete(tempPath);
        MessageBox.Show("File created.");
    }

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    //Method Enabled Controls:
    private void tEnabled(bool state)
    {
        tCompany.Enabled = state;
        tFirstname.Enabled = state;
        tPhone.Enabled = state;
        tLastname.Enabled = state;
        btnLogo.Enabled = state;
    }

    //Load the Template Document:
    private void button1_Click(object sender, EventArgs e)
    {
        if (LoadDoc.ShowDialog() == DialogResult.OK)
        {
            tFilename.Text = LoadDoc.FileName;
            tEnabled(true);
        }
    }

    //Load the logo picture :
    private void btnLogo_Click(object sender, EventArgs e)
    {
        if (LoadPic.ShowDialog() == DialogResult.OK)  //LoadPic is a OpenFileDialogue placed on the designer.
        {
            pathImage = LoadPic.FileName;
            btnSubmit.Enabled = true;
        }
    }

    //Create your document:
    private void btnSave_Click(object sender, EventArgs e)
    {
        if (SaveDoc.ShowDialog() == DialogResult.OK) //SaveDoc is a SaveFileDialogue placed on the designer.
        {
            CreateWordDocument(tFilename.Text, SaveDoc.FileName, pathImage);
            tEnabled(false);
        }
    }

}

有人对如何处理这个问题有什么想法吗?提前谢谢。

你为什么要这个?您希望实现的共同点是什么?[1]当用户第一次保存文件时,SFD将返回带有用户选择的文件名的文件路径。将此路径存储在某个变量中。[2] 再次单击保存按钮时,检查此路径变量,如果它有值,则跳过SFD并执行更新逻辑。[3] 当用户/文件会话完成时,在代码中的适当位置清除此path变量。@vnikhil谢谢您的回复。我一直在看。我已经创建了文件路径。字符串FilePath=Path.CombineAppDomain.CurrentDomain.BaseDirectory,文件名;所以我有,我认为这会起作用,但我如何应用它呢?我找到了一个例子,但它使用StreamWriter,我使用的是Microsoft Word。我不知道如何执行步骤2和步骤3。如果你有一个例子可以参考,那就太好了。