简体   繁体   中英

Filestream will not write xmldoc that contains an item from OpenFileDialog

I have this code that takes a file path that is obtained with the OpenFileDialog and attempts to save it to an xml file. For some reason the xml doc will not get written if one of the nodes contains a string from this open file dialog. An exception will not get thrown and the app will not crash, just the file will not get written.

If I use a string literal in place of the m_strSoundFile with the same contents, the xml document will get written correctly. So it has nothing to do with the '\\' character being illegal, which is what I initially thought. Maybe it has something to do with the fact that the OpenFileDialog is Win32? Any help would be appreciated.

Thanks, Alex

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
    string m_strSoundFile;

    public MainWindow()
    {
        InitializeComponent();
    }

    private void btnChooseFile_Click(object sender, RoutedEventArgs e)
    {
        Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
        dlg.Filter = "Wav files (*.wav)|*.wav"; // Filter files by extension
        dlg.InitialDirectory = @"C:\windows\media";

        Nullable<bool> result = true;
        bool pathExists = false;
        do
        {
            result = dlg.ShowDialog();

            if (result == true)
            {
                pathExists = dlg.CheckPathExists;
                if (!pathExists)
                    MessageBox.Show("Path does not exist");
                else
                    m_strSoundFile = dlg.FileName;
            }

        } while (result == true && !pathExists);

        m_tbFilename.Text = m_strSoundFile;
    }

    private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        XmlDocument xmlDoc = new XmlDocument();
        XmlNode xmlRootNode = xmlDoc.CreateElement("Settings");

        XmlNode node = xmlDoc.CreateElement("File");
        XmlAttribute a = xmlDoc.CreateAttribute("Path");
        a.Value = m_strSoundFile;

        node.Attributes.Append(a);

        xmlRootNode.AppendChild(node);
        xmlDoc.AppendChild(xmlRootNode);

        System.IO.FileStream fs;
        try
        {
            fs = System.IO.File.Open("configfile.xml", System.IO.FileMode.Create, System.IO.FileAccess.Write);
            xmlDoc.Save(XmlWriter.Create(fs, new XmlWriterSettings() { Indent = true, Encoding = Encoding.UTF8 }));
            fs.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }
}

Ok, I figured it out. After using an absolute path for the filestream it worked. It's still weird that it works conditionally when not using an absolute path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM