【问题标题】:File Not found in exe of wpf application在 wpf 应用程序的 exe 中找不到文件
【发布时间】:2013-12-27 09:57:43
【问题描述】:

发布 WPF 应用程序并生成 exe 时,我无法获取放置在模板文件夹中的文件。当我将文件夹和文件复制到 bin 时,它可以工作,或者如果使用

string StartUpPath = AppDomain.CurrentDomain.BaseDirectory.ToString();
//  var gparent = Directory.GetParent(Directory.GetParent(Directory.GetParent(StartUpPath).ToString()).ToString()).ToString();

ReportDocument reportDocument = new ReportDocument();
StreamReader reader = new StreamReader(new FileStream(StartUpPath + @"\Templates\Invoice.xaml", FileMode.Open));

此代码在我的本地运行良好,但是当我生成 exe 时,找不到这些文件。

【问题讨论】:

    标签: asp.net wpf


    【解决方案1】:

    那是因为您的文件不在 exe 中。我最近遇到了同样的问题。

    听起来你需要处理你的资源 - 有几种方法可以解决这个问题,微软有一个关于资源的full explanation here,包括使用 LinkedEmbedded 之间的区别 资源,以及指向其他优秀指南的链接。

    我假设您使用的是 Visual Studio,在这种情况下应该可以使用;

    • 右键单击无法访问的文件
    • 选择属性(或按ALT + ENTER
    • 构建操作设置为Resource
    • 复制到输出目录设置为Copy if newer
    • 保存

    ...你应该很高兴。

    【讨论】:

      【解决方案2】:

      我认为它会引发异常,因为该文件在该机器上不存在。您可以在属性窗口中将Build Action 设置为Content,将Copy to Output Directory 设置为Copy AlwaysCopy If Newer

      这些设置将确保您有文件到您的输出。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多