【问题标题】:C# how to get .pdf file names only from folder without extension?C#如何仅从没有扩展名的文件夹中获取.pdf文件名?
【发布时间】:2018-12-06 21:16:17
【问题描述】:

我得到了以下代码来从临时文件夹中获取 .pdf 文件名,但是如何从中删除扩展名?似乎 test.GetFileNameWithoutExtension("*.pdf") 不起作用。请帮忙。

            DirectoryInfo test = new DirectoryInfo(@"C:\temp"); 
            FileInfo[] Files = test.GetFiles("*.pdf"); 

            comboBox1.DataSource = Files;
            comboBox1.DisplayMember = "Name";

【问题讨论】:

标签: c#


【解决方案1】:
var fileNames = Files.Select(f => Path.GetFileNameWithoutExtension(f.Name)).ToList();
combobox1.DataSource = fileNames;

【讨论】:

    【解决方案2】:

    GetFileNameWithoutExtension()System.IO.Path的方法:

    string[] Files = test.GetFiles("*.pdf")
         .Select(x => x => Path.GetFileNameWithoutExtension(x.FullName))
         .ToArray(); 
    

    【讨论】:

      【解决方案3】:

      Path 类是你的朋友:

      使用Path.Get​File​Name​Without​Extension

      https://docs.microsoft.com/en-us/dotnet/api/system.io.path.getfilenamewithoutextension?view=netframework-4.7.2

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-05-15
        • 1970-01-01
        • 1970-01-01
        • 2014-06-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多