XML 文件抛出文件未找到异常,可能是由 Outlook 引起的
当 Outlook 调用我的应用程序时,我收到文件未找到异常。当电子邮件保存到计算机时,会调用该应用程序并对保存的消息执行操作。
我的应用程序使用 XML 文件来存储可配置设置,但当 Outlook 调用执行应用程序时找不到此文件。如果我手动运行该程序,那么它工作正常。
关于异常的有趣之处在于:
System.IO.FileNotFoundException:找不到文件“C:\ Program Files \ Common Files \ System \ MSMAPI \ 1033 \ settingsOpened.xml”。
为什么 Outlook 认为该文件位于此处?这不是文件的路径,但我确信与 Outlook 有关。我在代码中引用路径的方式就是:
XmlDocument xDoc = new XmlDocument();
xDoc.Load("settingsOpened.xml");
文件与 .exe 位于同一文件夹中。我也不想对 XML 文件的完整路径进行硬编码。
任何帮助将不胜感激。
谢谢, 罗斯
I'm getting a file not found exception when my application is called by Outlook. It's called when an email is saved to the computer the app is called and performs an action on the saved message.
My app uses a XML file to store configurable settings but this file can't be found when Outlook calls to executes the application. If I run the program manually then it works fine.
The interesting thing about the exception is this:
System.IO.FileNotFoundException: Could not find file 'C:\Program Files\Common Files\System\MSMAPI\1033\settingsOpened.xml'.
Why does Outlook think that the file is here ? This isn't the path for the file but I'm sure is related to Outlook. The way I'm referencing the path in the code is just:
XmlDocument xDoc = new XmlDocument();
xDoc.Load("settingsOpened.xml");
With the file being in the same folder as the .exe. I don't want to hard code the full path in for the XML files either.
Any help would be greatly appreciated.
Thanks,
Ross
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从显示的代码中,我假设它将当前路径作为查找文件的位置。当前路径有点不可预测,因为某些操作会影响其值,并且该值在每次调用时都会保留。即,当应用程序的另一部分,甚至另一个应用程序设置当前路径时,下次将使用该值。要设置当前路径,使用通用对话框浏览某个文件就足够了。
在您的情况下,我会尝试
Environment.GetFolderPath
和Environment.SpecialFolder
)或
要查找
myType
的程序集路径,您可以使用以下代码:无论哪种情况,您都应该考虑到在较新的 Windows 操作系统中,用户没有对系统驱动器的所有路径的写访问权限。
From the code shown I would assume that it takes the current path as the location to look for the file. The current path is a bit unpredictable as certain operations effect its value and the value is persisted on each call. I.e. when another part of your application, or even another applciation, sets the current path this value is used the next time around. To set the current path it is enought to use a common dialog to browse for a certain file.
In your case I'd try to either to
Environment.GetFolderPath
andEnvironment.SpecialFolder
)or
To find the assembly path for
myType
you can use the following code:In either case you should consider that in newer windows operating systems the user does not have write access to all paths of the system drive.