c# - 如何在控制台应用程序 .NET Core (C#) 中制作打开文件对话框?

我想提供一个从电脑的任何地方选择文件的选项。目前我明确给出了这样的路径:

FileInfo existingFile = new FileInfo(@"C:\Users\User_name\Downloads\bank_statement.xlsx");

使用 EPPlus 操作 excel 文件。如何直接从所需文件夹中获取文件? 控制台应用程序 .NET Core 3.1 C#。

最佳答案

如果您真的想在没有依赖项的控制台应用程序中打开一个对话框(并且命令行参数不是一个选项)您可以在 comdlg32 中调用 GetOpenFileName .dll。 pinvoke.net为这些方法及其参数提供 C# 定义。当然,这取决于平台(仅限 Windows)。

using System;
using System.Linq;
using System.Collections.Generic;
using System.Runtime.InteropServices;

namespace DemoApp
{
    // From https://www.pinvoke.net/default.aspx/Structures/OPENFILENAME.html
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
    public struct OpenFileName
    {
        public int lStructSize;
        public IntPtr hwndOwner;
        public IntPtr hInstance;
        public string lpstrFilter;
        public string lpstrCustomFilter;
        public int nMaxCustFilter;
        public int nFilterIndex;
        public string lpstrFile;
        public int nMaxFile;
        public string lpstrFileTitle;
        public int nMaxFileTitle;
        public string lpstrInitialDir;
        public string lpstrTitle;
        public int Flags;
        public short nFileOffset;
        public short nFileExtension;
        public string lpstrDefExt;
        public IntPtr lCustData;
        public IntPtr lpfnHook;
        public string lpTemplateName;
        public IntPtr pvReserved;
        public int dwReserved;
        public int flagsEx;
    }

    public class Program
    {
        // From https://www.pinvoke.net/default.aspx/comdlg32/GetOpenFileName.html
        [DllImport("comdlg32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        private static extern bool GetOpenFileName(ref OpenFileName ofn);

        private static string ShowDialog()
        {
            var ofn = new OpenFileName();
            ofn.lStructSize = Marshal.SizeOf(ofn);
            // Define Filter for your extensions (Excel, ...)
            ofn.lpstrFilter = "Excel Files (*.xlsx)\0*.xlsx\0All Files (*.*)\0*.*\0";
            ofn.lpstrFile = new string(new char[256]);
            ofn.nMaxFile = ofn.lpstrFile.Length;
            ofn.lpstrFileTitle = new string(new char[64]);
            ofn.nMaxFileTitle = ofn.lpstrFileTitle.Length;
            ofn.lpstrTitle = "Open File Dialog...";
            if (GetOpenFileName(ref ofn))
                return ofn.lpstrFile;
            return string.Empty;
        }

        public static void Main(string[] args)
        {
            var filename = ShowDialog();
            Console.WriteLine(filename);
        }
    }
}

https://stackoverflow.com/questions/68711769/

相关文章:

javascript - 如何使用 Axios 拦截器向响应添加一些 header ?

css - 注入(inject)具有特定范围的 Chakra UI 全局样式

apache-spark - 如何在 Spark SQL 中访问 python 变量?

c++ - 使用 accumulate 计算替代总和

r - 按类别获取最大值作为 R 中的新列

bash - 引用命令替换的正确方法

java - 使用同步(锁定对象) block 时,静态锁定对象是否需要是最终的?

python - 从需要登录的社交媒体网站抓取最新帖子时,如何保持不被发现?

reactjs - 如何在 onSubmit 事件中使用 react-query useQuery?

python - 根据python中另一个字符串元素的数量删除字符串元素