我正在 Windows 6.5.3 中开发小型应用程序。为此,我需要从 C 盘读取一个 XML 文件。当我尝试读取文件时,出现以下错误。我检查了文件访问。没关系。
“价值不在预期范围内。”
堆栈跟踪
at System.IO.__Error.WinIOError(Int32 errorCode, String str)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, Boolean useAsync, String msgPath)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials)
at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)
at System.Xml.XmlReader.Create(String inputUri, XmlReaderSettings settings, XmlParserContext inputContext)
at System.Xml.XmlReader.Create(String inputUri, XmlReaderSettings settings)
at System.Xml.Linq.XElement.Load(String uri, LoadOptions options)
at FREEMobile.frmMainMenu.frmMainMenu_Load(Object sender, EventArgs e)
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form._SetVisibleNotify(Boolean fVis)
at System.Windows.Forms.Control.set_Visible(Boolean value)
at System.Windows.Forms.Application.Run(Form fm)
at FREEMobile.frmMainMenu.Main()
代码
Dim faciltyXML As XElement
Dim strFileName As String = "C:\Users\xs1969\Desktop\FREEMobile\FREEMobile\Facility.xml"
faciltyXML = XElement.Load(strFileName, LoadOptions.None)
编辑1:
谢谢CTACKE。我做了以下更改。它工作正常。
Dim faciltyXML As XElement
Dim strAppDir As String = Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase)
Dim strFullPathToMyFile As String = Path.Combine(strAppDir, "Facility.xml")
faciltyXML = XElement.Load(strFullPathToMyFile)