C# Windows 窗体:另存为问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6617684/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 17:20:31  来源:igfitidea点击:

C# Windows Forms: Save & Save As Woes

c#windowswinforms

提问by jonalodev

This is really frustrating me. I'm new to C Sharp so looking for some assistance. My Save/Save As is totally fubar.

这真的让我很沮丧。我是 C Sharp 的新手,因此需要一些帮助。我的保存/另存为完全是 fubar。

Two questions really:

真的有两个问题:

How do I save changes to an existing file without popping a save dialog? If I click save it pops a dialog which is fine so I save it, then make some changes and click Save again it pops a dialog rather than just saving the file to the name already given.

如何在不弹出保存对话框的情况下保存对现有文件的更改?如果我单击保存它会弹出一个对话框,这很好,所以我保存它,然后进行一些更改并再次单击保存它会弹出一个对话框,而不仅仅是将文件保存到已经给定的名称。

How do I show the filename rather than the full path in the save as dialog? It shows as File Name: C:\Users\username\desktop\save\filename.xml

如何在另存为对话框中显示文件名而不是完整路径?它显示为文件名:C:\Users\username\desktop\save\filename.xml

This is in MainForm.cs.

这是在 MainForm.cs 中。

private void biFileSave_Click(object sender, EventArgs e)
    {
        // Save diagram
        EditorForm editForm = this.ActiveDiagramForm;
        if (editForm != null)
        {
            if (!editForm.HasFileName)
            {
                if (this.saveEditorDialog.ShowDialog(this) == DialogResult.OK)
                {
                    this.ActiveDiagram.SaveSoap(this.saveEditorDialog.FileName);
                }
            }
            else
            {
                this.ActiveDiagram.SaveSoap(this.saveEditorDialog.FileName);
            }
        }

private void biFileSaveAs_Click(object sender, EventArgs e)
    {
        // Save As diagram
        EditorForm editForm = this.ActiveDiagramForm;
        if (editForm != null)
        {
            if (editForm.HasFileName)
            {
                this.saveEditorDialog.FileName = editForm.FileName;
            }
            if (this.saveEditorDialog.ShowDialog(this) == DialogResult.OK)
            {
                this.ActiveDiagram.SaveSoap(this.saveEditorDialog.FileName);
                string strFileName = this.saveEditorDialog.FileName;

            }

        }
    }

This is in EditForm.cs

这是在 EditForm.cs

 public string FileName
    {
        get
        {
            return this.fileName;
        }
        set
        {
            this.fileName = value;
            this.Text = Path.GetFileNameWithoutExtension(this.fileName);
        }
    }

    public bool HasFileName
    {
        get
        {
            return (this.fileName != null && this.fileName.Length > 0);
        }
    }

EDIT:

编辑:

Thank you for helping me on this so quickly! My Save works as expected now, however it introduced a strange issue with Save As (code above).

谢谢你这么快就帮我解决了这个问题!我的保存现在按预期工作,但是它在另存为(上面的代码)中引入了一个奇怪的问题。

If I open a file (test.xml) that I have saved, then click Save As (name it test2.xml) it saves to the new file. BUT, when I open that test.xml again and make changes and click Save it saves those changes to the test2.xml. Very strange...any ideas?

如果我打开一个已保存的文件 (test.xml),然后单击另存为(将其命名为 test2.xml),它将保存到新文件中。但是,当我再次打开那个 test.xml 并进行更改并单击保存时,它会将这些更改保存到 test2.xml。很奇怪……有什么想法吗?

采纳答案by sdcoder

Where in code is FileName set? From the sample you've posted, I don't see it being set anywhere, but perhaps it is elsewhere. This may work:

FileName 在代码中的哪里设置?从您发布的示例中,我没有看到它被设置在任何地方,但也许它在其他地方。这可能有效:

private void biFileSave_Click(object sender, EventArgs e)
    {
        // Save diagram
        EditorForm editForm = this.ActiveDiagramForm;
        if (editForm != null)
        {
            if (!editForm.HasFileName)
            {
                if (this.saveEditorDialog.ShowDialog(this) == DialogResult.OK)
                {
                    this.ActiveDiagram.SaveSoap(this.saveEditorDialog.FileName);
                    editForm.FileName = this.saveEditorDialog.FileName;
                }
            }
            else
            {
                this.ActiveDiagram.SaveSoap(this.saveEditorDialog.FileName);
            }
        }

回答by keyboardP

1) The Save dialog boxwill simply return the file path the user wishes to save to. Using this path, you can then perform your save function. If you want to save to the current document, simply skip the dialog box and perform your save function with a cached version of the chosen path.

1) Savedialog box将简单地返回用户希望保存到的文件路径。使用此路径,您可以执行保存功能。如果要保存到当前文档,只需跳过对话框并使用所选路径的缓存版本执行保存功能。

For example, in your form, have a variable:

例如,在您的表单中,有一个变量:

string currentFilePath = "";

When the user first opens a Save Dialog Box, fill that variable with the path the user chose. The next time the user saves (instead of save as), perform a check:

当用户第一次打开保存对话框时,用用户选择的路径填充该变量。下次用户保存(而不是save as)时,执行检查:

if(!String.IsNullOrEmpty(currentFilePath))
   //save method using currentFilePath as the path to save to

2) You need to set the FileNamesomewhere. You can then use Path.GetFileNameon the FileNameto get just the name and extension.

2)您需要设置FileName某个地方。然后,您可以使用Path.GetFileNameFileName得到公正的名和扩展名。