Domanda

I'm wondering how you got about saving a file in winform to the target location without having to hard code in the location.

Right now, my save methods look like this:

public void GenereateSettingsFile(List<Node> nodeList)
{
    string filePath = "Desktop\\Save.xml";
    _rootNode.RemoveChild(_userNode);

    _userNode = _xmlDoc.CreateElement("Display_Settings");
    _rootNode.AppendChild(_userNode);

    foreach (Node n in nodeList)
    {
        foreach (XmlElement e in n.GenerateXML(_xmlDoc))
        {

            _userNode.AppendChild(e);
        }
    }

    _xmlDoc.Save(filePath);
}

public void SaveXML(string location)
{
    _xmlDoc.Save(location);
}

This is called when I hit the save button like so:

private void saveToolStripMenuItem_Click(object sender, EventArgs e)
{
    foo.GenereateSettingsFile(_nodeList);
}

Now, with my save as function I've got it so a new window pops up and I can browse to my location where I wish to save my file with this code:

private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
    using (SaveFileDialog dialog = new SaveFileDialog())
    {
        dialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
        dialog.FilterIndex = 2;
        dialog.RestoreDirectory = true;

        if (dialog.ShowDialog() == DialogResult.OK)
        {
            // Can use dialog.FileName
            using (Stream stream = dialog.OpenFile())
            {
                // Save data
                inmo.GenereateSettingsFile(_nodeList);
            }
        }
    }
}

But, this is still calling my old save function which is telling my program to save in the desktop. Is there a way I can pass in the file location my save as window generates to my save function?

È stato utile?

Soluzione

Just promote filePath to be a parameter of GenereateSettingsFile.

private void saveAsToolStripMenuItem_Click(object sender, EventArgs e)
{
    using (SaveFileDialog dialog = new SaveFileDialog())
    {
        dialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
        dialog.FilterIndex = 2;
        dialog.RestoreDirectory = true;

        if (dialog.ShowDialog() == DialogResult.OK)
        {
            // Can use dialog.FileName
            //using (Stream stream = dialog.OpenFile())
            //{
            // Save data
            inmo.GenereateSettingsFile(_nodeList, dialog.FileName);
            //}
        }
    }
}

public void GenereateSettingsFile(List<Node> nodeList, string filePath)
{
    //string filePath = "Desktop\\Save.xml";
    _rootNode.RemoveChild(_userNode);

    _userNode = _xmlDoc.CreateElement("Display_Settings");
    _rootNode.AppendChild(_userNode);

    foreach (Node n in nodeList)
    {
        foreach (XmlElement e in n.GenerateXML(_xmlDoc))
        {

            _userNode.AppendChild(e);
        }
    }

    _xmlDoc.Save(filePath);
}

Altri suggerimenti

Try

dialog.InitialDirectory = myPreferredDirectory;

See SaveFileDialog.InitialDirectory for more info

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top