0
我正在測試應用程序以從XML文件(工作)讀取並保存到XML文件(不工作)。無法保存到嵌入式資源中的XML文件
這是我的代碼不起作用。有想法該怎麼解決這個嗎?我需要能夠保存到XML文件。我得到的錯誤是:流未寫
代碼:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim strTime As String
strTime = Now.ToString
Dim _assembly As Assembly
_assembly = [Assembly].GetExecutingAssembly()
Dim FileName = _assembly.GetManifestResourceStream("EmbeddedResource.xml")
Try
Dim xmlData As New XmlDocument
Dim nodeRoot, nodeTroubleInfo, nodeDateTimeSaved As XmlNode
xmlData.Load(FileName)
nodeRoot = xmlData.SelectSingleNode("//DATA")
nodeTroubleInfo = nodeRoot.SelectSingleNode("//TroubleInfo")
nodeTroubleInfo.InnerText = txtNotes.Text
nodeDateTimeSaved = nodeRoot.SelectSingleNode("//DateTimeSaved")
nodeDateTimeSaved.InnerText = strTime
xmlData.Save(FileName)
MsgBox("SAVE Button Pressed!", MsgBoxStyle.Information, "Note saved successfully!")
Catch ex As Exception
MsgBox("Error saving note. The error was: " & vbCrLf & Err.Description, MsgBoxStyle.Exclamation, "Error saving information to file.")
Exit Sub
End Try
End Sub
我遇到的問題是如果我在Program Files目錄中創建一個新的XML文件,我必須以管理員權限運行我的應用程序。在哪裏/我該如何處理xml文件? – CJSoldier
將它寫入程序的Application Data文件夾。注意有一個'C:\ Users \用戶名\ AppData \ Local \ Microsoft \ VisualStudio'文件夾?在安裝過程中創建一個'C:\ Users \用戶名\ AppData \ Local \ yourCompany \ yourProduct'文件夾。 –
謝謝@john這個技巧。謝謝! – CJSoldier