XML 文件抛出文件未找到异常,可能是由 Outlook 引起的

发布于 2024-09-24 10:47:00 字数 552 浏览 8 评论 0原文

当 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

魔法唧唧 2024-10-01 10:47:00

从显示的代码中,我假设它将当前路径作为查找文件的位置。当前路径有点不可预测,因为某些操作会影响其值,并且该值在每次调用时都会保留。即,当应用程序的另一部分,甚至另一个应用程序设置当前路径时,下次将使用该值。要设置当前路径,使用通用对话框浏览某个文件就足够了。

在您的情况下,我会尝试

  • 通过从众所周知的文件夹之一派生来显式指定路径(例如用户的应用程序路径 - 查找 Environment.GetFolderPathEnvironment.SpecialFolder

  • 解析相对于 Dll 程序集路径的路径。

要查找 myType 的程序集路径,您可以使用以下代码:

String strPath = System.IO.Path.GetDirectoryName(typeof(myType).Assembly.CodeBase);

无论哪种情况,您都应该考虑到在较新的 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

  • specify the path explicitly by deriving it from one of the well known folders (e.g. the user's app path - look for Environment.GetFolderPath and Environment.SpecialFolder)

or

  • to resolve the path relative to your Dll's assembly path.

To find the assembly path for myType you can use the following code:

String strPath = System.IO.Path.GetDirectoryName(typeof(myType).Assembly.CodeBase);

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文