OpenFileDialog로 얻은 파일 경로를 XML 파일에 저장하려고 시도하는 코드가 있습니다. 노드 중 하나에이 열린 파일 대화 상자의 문자열이 포함되어 있으면 XML 문서에 어떤 이유로 든 기록되지 않습니다. 예외가 throw되지 않고 앱이 충돌하지 않고 파일이 기록되지 않습니다.Filestream은 OpenFileDialog의 항목이 들어있는 xmldoc을 작성하지 않습니다.
동일한 내용의 m_strSoundFile 대신 문자열 리터럴을 사용하면 XML 문서가 올바르게 작성됩니다. 그래서 그것은 '\'문자가 불법 인 것과 아무 상관이 없습니다. 그것은 제가 처음에 생각한 것입니다. 어쩌면 OpenFileDialog가 Win32라는 사실과 관련이 있을까요? 어떤 도움을 주시면 감사하겠습니다.
감사합니다, 알렉스 확인
/// <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);
}
}
}
"쓰지 않을 것"이라고 말하면 코드에서 오류가 발생 했습니까? 아니면 코드가 자동으로 실패합니까? 오류 일 경우 오류 메시지를 포함 할 수 있습니까? –
@DanPuzey 예외가 throw되지 않고 코드가 완전히 실행되고 채우기가 작성되지 않습니다. 감사! – Alex
@Alex, 코드가 작동합니다. 어떤 예외가 있습니까? 'File.Open' 메서드에 올바른 경로를 전달합니까? –