开发者

Save the file in C#

I have a tree view and a button and a toolbox. I will create the tree by dragging and dropping items from toolbox. At the end, i will store the tree structure as an xml file. For this, when I click on the button, it prompts for save dialog and once the user select the path to store, I am able to save the xml file successfully. Now, if I want to save the structure without opening the save dialog every time(whenever I make changes to tree structure) when I ma开发者_运维知识库ke changes(except first time) like in MS word, how can I achieve it?


Just save the file name you chose in the dialog in a variable. Next time check if you have the file name, if you do simply save the file.


If you are "dragging and dropping" the SaveFileDialog on your form, the SaveFileDialog object will have the FileName set until you close the application. There is no need to save to a field in your forms class. Though it is basically the same.

    private SaveFileDialog saveFileDialog = null;

    private string GetFileName()
    {
        if (saveFileDialog.FileName != String.IsNullOrEmpty(saveFileDialog.FileName))
        {
            return saveFileDialog.FileName;
        }
        else
        {
            if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                return saveFileDialog.FileName;
            }
            else
            {
                //throw exception or something similar
            }
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜