WinForms如何打开资源文件夹中的文件

WinForms how to open file in resources folder

我是 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 文件,该文件是我的应用程序中的嵌入式资源

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 类。

您目前只输出基本目录。除此之外,您只在 基本目录 中查找文件。执行从基目录发生,所以你的程序正在寻找 ..\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 ...
        }