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

How to make a openfile dialog box in console application .NET Core (C#)?

我想提供一个选项,让您可以从电脑的任何位置选择文件。目前我明确给出了这样的路径:

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

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

如果您真的想要在没有依赖项的控制台应用程序中打开一个对话框(命令行参数不是一个选项)您可以调用GetOpenFileNamecomdlg32.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)[=10=]*.xlsx[=10=]All Files (*.*)[=10=]*.*[=10=]";
            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);
        }
    }
}

如果确实需要在控制台应用程序中使用 .NET GUI 组件,您应该将其转换为 UI 应用程序。 但之后应用程序变成了GUI应用程序(不是控制台)

例如,您可以转换为 WinForms 应用程序。

打开项目文件并添加<UseWindowsForms>true</UseWindowsForms>:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <UseWindowsForms>true</UseWindowsForms>
  </PropertyGroup>
</Project>

然后:

using System;
using System.Windows.Forms;

namespace ConsoleApp
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            OpenFileDialog dialog = new OpenFileDialog();
            if (DialogResult.OK == dialog.ShowDialog())
            {
                string path = dialog.FileName;
            }
            Console.WriteLine("Hello World!");
        }
    }
}