首页 > 解决方案 > WinForms如何打开资源文件夹中的文件

问题描述

我是 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");
        }
    }
}

请指导我完成它。

标签: c#winforms

解决方案


我在我的一个应用程序中执行此操作以打开一个 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类。


推荐阅读