【问题标题】:How to get only (excel) filename in a folder using C# console application如何使用 C# 控制台应用程序在文件夹中仅获取(excel)文件名
【发布时间】:2021-07-12 17:11:39
【问题描述】:

是否可以仅获取文件夹中的 Excel 文件名列表(例如:2021070701.CSV)?

当我使用此“Directory.GetFiles”时,我得到了 .csv excel 的完整路径,但我只想过滤带扩展名的 excel 文件名(例如:2021070701.CSV)

我使用“FileInfo fi = new FileInfo();”但我没有得到正确的解决方案。

         public static void getExcelFileName()
        {

            string[] filename = Directory.GetFiles(@"C:\Users\Ashok 
                  Kumar\OneDrive\Desktop\Ashok\MarketPrice\NSE\Futures\Live", "*.csv");

            foreach (var item in filename)
            {
                Console.WriteLine(item);
            }
        }

这是我要走的路

帮我解决这个问题,我是编码新手。

【问题讨论】:

标签: c# console-application


【解决方案1】:

您可以使用Path 类中的GetFileNameWithoutExtension() 方法来获取文件的名称。

public static void getExcelFileName()
        {

            string[] filename = Directory.GetFiles(@"C:\Users\Ashok 
                  Kumar\OneDrive\Desktop\Ashok\MarketPrice\NSE\Futures\Live", "*.csv");

            foreach (var item in filename)
            {
                Console.WriteLine(Path.GetFileNameWithoutExtension(item));
            }
        }

【讨论】:

    【解决方案2】:

    当然,您可以致电 Path.GetFileName(string),它会返回您所要求的内容!

            public static void getExcelFileName()
            {
                string[] filesPaths = Directory.GetFiles(@"C:\Users\Ashok Kumar\OneDrive\Desktop\Ashok\MarketPrice\NSE\Futures\Live",
                    "*.csv");
    
                foreach (var filePath in filesPaths)
                {
                    Console.WriteLine(Path.GetFileName(filePath));
                }
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-22
      • 1970-01-01
      • 2023-03-30
      • 2019-10-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多