如何获得C#folderbrowserdialog只有没有扩展名的文件名 [英] How to get C# folderbrowserdialog only filename without extension

查看:133
本文介绍了如何获得C#folderbrowserdialog只有没有扩展名的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码:

string folderName = this.folderBrowserDialog1.SelectedPath;

foreach(Directory.GetFiles(folderName)中的字符串f)

{

if(f.ToLower()。EndsWith(。jpg)== true || f.ToLower()。EndsWith(。jpeg)== true)

{

this.listBox1.Items.Add(f);



但是要获得完整的Filepath。如何才能获得没有扩展名的文件名?



我尝试了什么:



代码:
string folderName = this.folderBrowserDialog1.SelectedPath;
foreach(Directory.GetFiles(folderName)中的字符串f)
{
if(f.ToLower()。EndsWith(。jpg)== true || f.ToLower() .EndsWith(。jpeg)== true)
{
this.listBox1.Items.Add(f);

解决方案

尝试:

 string folderName = this.folderBrowserDialog1.SelectedPath; 
foreach(Directory.GetFiles(folderName)中的字符串f)
{
if(f.ToLower()。EndsWith(。jpg)|| f.ToLower()。EndsWith( 。jpeg))
{
this.listBox1.Items.Add(Path.GetFileNameWithoutExtension(f));


GetFiles [ ^ ]总是返回一个全名数组 - 你不能改变它......

你可以使用 Path.GetFileNameWithoutExtension [ ^ ],单独获取文件名。

Code:
string folderName = this.folderBrowserDialog1.SelectedPath;
foreach (string f in Directory.GetFiles(folderName))
{
if (f.ToLower().EndsWith(".jpg")==true || f.ToLower().EndsWith(".jpeg")==true)
{
this.listBox1.Items.Add(f);

But get full Filepath. How can i get only filename without extension?

What I have tried:

Code:
string folderName = this.folderBrowserDialog1.SelectedPath;
                foreach (string f in Directory.GetFiles(folderName))
                {
                    if (f.ToLower().EndsWith(".jpg")==true || f.ToLower().EndsWith(".jpeg")==true)
                    {
                        this.listBox1.Items.Add(f);

解决方案

Try:

string folderName = this.folderBrowserDialog1.SelectedPath;
     foreach (string f in Directory.GetFiles(folderName))
     {
         if (f.ToLower().EndsWith(".jpg") || f.ToLower().EndsWith(".jpeg"))
         {
             this.listBox1.Items.Add(Path.GetFileNameWithoutExtension(f));


GetFiles[^] always returns an array of full names - you can not change that...
You can use Path.GetFileNameWithoutExtension[^], to get the file name alone...


这篇关于如何获得C#folderbrowserdialog只有没有扩展名的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆