【问题标题】:Cannot access files in output directory when running under Desktop Bridge在桌面桥下运行时无法访问输出目录中的文件
【发布时间】:2019-04-23 07:12:18
【问题描述】:

在我的 WPF 项目中,我有一些 JSON 文件设置为内​​容/复制到输出文件夹。当作为标准 WPF 运行时,我按如下方式访问它们并且工作正常。

foreach (var config in Directory.GetFiles("HostConfigs", "*.json"))

但是当我使用打包项目在桌面桥下运行应用程序时,它会抛出以下异常

System.IO.DirectoryNotFoundException: '找不到路径'C:\WINDOWS\SysWOW64\HostConfigs'的一部分。'

【问题讨论】:

    标签: wpf desktop-bridge


    【解决方案1】:

    Desktop Bridge 项目不会自动将当前目录设置为项目的输出文件夹...而是使用 Windows 的默认目录。

    要在整个项目中解决此问题,在主启动点 (App.xaml.cs),只需添加以下内容...

        public partial class App : Application
        {
            public App()
            {
                SetCurrentDirectory();
            }
    
            /// <summary>
            /// Sets the current directory to the app's output directory. This is needed for Desktop Bridge, which
            /// defaults to the Windows directory.
            /// </summary>
            private void SetCurrentDirectory()
            {
                // Gets the location of the EXE, including the EXE name
                var exePath = typeof(App).Assembly.Location;
                var outputDir = Path.GetDirectoryName(exePath);
                Directory.SetCurrentDirectory(outputDir);
            }
        }
    

    【讨论】:

    猜你喜欢
    • 2021-01-29
    • 2012-10-09
    • 2012-06-30
    • 1970-01-01
    • 2019-05-29
    • 2018-03-12
    • 1970-01-01
    • 2012-09-22
    • 1970-01-01
    相关资源
    最近更新 更多