【问题标题】:WinForms how to open file in resources folderWinForms如何打开资源文件夹中的文件
【发布时间】:2021-08-11 13:59:25
【问题描述】:

我是 WinForms 技术的新手。我正在使用 .NET Framework 4.8 ,Microsoft Visual Studio 2019。我将文件放在Resources 文件夹中。

我试过这样的

using DevExpress.XtraBars;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace accwf
{
    public partial class NhapSoDu : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public NhapSoDu()
        {
            InitializeComponent();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            Console.WriteLine(System.AppDomain.CurrentDomain.BaseDirectory);
            Process.Start(".../B01-DN_01_Summary.xlsx");
        }
    }
}

请指导我完成它。

【问题讨论】:

  • 项目中那些文件的属性是什么?你是把它们作为资源嵌入,你是在复制它们吗?
  • 我将它复制到项目中。如果这种方式不是好的做法,请指导我的最佳做法。
  • 请原谅我的无知,但是,与实际的“xlsx”文件本身相比,将文件 PATHS 嵌入到应用程序资源中似乎更明智。这样做似乎很奇怪,因为嵌入式文件在不重新编译代码的情况下永远不会改变。如果数据是静态的并且永远不会改变,那么我认为将数据存储到开销较小的东西中可能会更好。只是一个想法。
  • 我的问题与文件属性有关。如果您在Solution Explorer 中右键单击该文件并选择Properties,您将看到一个属性窗口。在 Advanced 下,有两个有用的属性:Build ActionCopy to Output。如果要将文件作为资源嵌入到输出应用程序中,请将 Build Action 设为 Embedded Resource(并将 copy 属性设为 Do Not Copy)。如果您希望将文件复制到输出文件夹(或某个子文件夹),请选择 Build Action 作为 Content 并且复制选项是两个复制选项之一

标签: c# winforms


【解决方案1】:

我在我的一个应用程序中执行此操作以打开一个 XLSX 文件,该文件是我的应用程序中的嵌入式资源

private void buttonOpenTemplate_Click(object sender, EventArgs e)
{
    byte[] templateFile = Properties.Resources._01__So_du_tai_khoan; // This is your Excel document in the application Resources
    string tempPath = $"{Path.GetTempFileName()}.xlsx";
    using (MemoryStream ms = new MemoryStream(templateFile))
    {
        using(FileStream fs = new FileStream(tempPath, FileMode.OpenOrCreate))
        {
            ms.WriteTo(fs);
            fs.Close();
        }
        ms.Close();
    }
    Process.Start(tempPath);
}

这需要引用System.IO 才能访问MemoryStreamFileStream 类。

【讨论】:

【解决方案2】:

您当前仅输出基本目录。除此之外,您只需要在该基本目录中查找文件。执行从基目录开始,因此您的程序正在寻找 ..\Path\to\exe\B01-DN_01_Summary.xlsx 而它应该在寻找 ..\Path\to\ exe\Resources\FilesHere\ImportExcel\B01-DN_01_Summary.xlsx

注意:不建议将资源文件嵌入到您的应用程序中。最好存储它们的位置并允许应用程序遍历您的目录以找到指定的文件位置。

您可以尝试以下改编版本:

您需要确保将所需文件的 复制到输出目录 属性设置为“始终复制”或“如果较新则复制”。这将确保在您的输出目录中创建目录路径。

namespace accwf
{
    public partial class NhapSoDu : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        public NhapSoDu()
        {
            InitializeComponent();
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            string resourcePath = System.IO.File.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory, "Resources\\FilesHere\\ImportExcel\\B01-DN_01_Summary.xlsx")
            
            if (File.Exists(resourcePath))
            {
                MessageBox.Show("Exists");
            }
            else 
            {
                MessageBox.Show("Doesn't Exist");
            }
            
            Process.Start(resourcePath);
        }
    }
}

这是我如何获取帮助菜单的 PDF 文件文档的示例:

public void MyMethod()
        {
            // helpMenuPath is a global var set to something like: Area/MyApp/Resources/
            string filePath = helpMenuPath;
            string[] fileNames = new string[0]; // Initialize the variable with length of 0. Directory.GetFiles() will allow for this length to be overwritten 

            // Try/Catch in case bad dir
            try
            {
                fileNames = Directory.GetFiles(filePath);
            }
            catch (IOException ioe)
            {
                // error catch for if bad dir
                MessageBox.Show($"Error in getting files: {ioe.Message}");
            }

            // Do something with files ...
        }

【讨论】:

  • 第一个代码 sn-p 导致错误 user-images.githubusercontent.com/1328316/… 。第二个sn-ps,我用的是WinForms,不是ASP.NET Core。
  • @DoNhuVy ,请查看更新后的答案。第二个 sn-p 旨在用作获取文件的另一种方法的参考。您将忽略 ViewData[] 调用,将返回类型设置为 void 并删除 return View();
  • 因为我在 ASP.NET Core 中看起来很像。在 WinForms 中有 IActionResult 吗?
  • ASP.NET Core 中的第二个 sn-p is - 但是我提供了它作为示例。我已将第二个 sn-p 修改为更“WinFormsfriendly”
猜你喜欢
  • 2013-08-11
  • 2014-01-28
  • 2014-03-17
  • 1970-01-01
  • 2018-12-17
  • 2018-08-15
  • 2010-09-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多