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

标签 c# .net-core console-application openfiledialog .net-core-3.1

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

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

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

最佳答案

如果您真的想在没有依赖项的控制台应用程序中打开一个对话框(并且命令行参数不是一个选项)您可以在 comdlg32 中调用 GetOpenFileName .dllpinvoke.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);
        }
    }
}

关于c# - 如何在控制台应用程序 .NET Core (C#) 中制作打开文件对话框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68711769/

相关文章:

c# - 使用特定方法控制 XML 序列化格式

c# - 使用 TimeSpan 和 DateTime 计算

.net - 通过 REST 在 DocumentDb 中创建文档时未经授权

c# - C# 中的 EGP 货币

C# 如何在控制台应用程序执行期间禁用 linux 终端用户输入

c# - C# 控制台应用程序中的居中文本仅适用于某些输入

c# - 从 MySQL 到 MS Access 的 1GB 数据

c# - 将我的 .aspx 页面中的代码转换为 C# .cs 页面中的代码

amazon-web-services - AWS Cognito JWT 身份验证适用于 ID token ,但不适用于访问 token ?返回 401

.Net core 的 MySql 连接器将文本截断为 255 个字符