用vb.net获取文件夹名称 [英] Getting Folder names with vb.net

查看:141
本文介绍了用vb.net获取文件夹名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编码新手,我需要帮助仅获取一个目录中文件夹的名称.我可以获得文件夹的列表,但是它显示了完整路径,我只需要文件夹名称即可.

例子.

我可以得到这个:

C:\ Program Files(x86)\ Adob​​e

但我只想要:

ADOBE

我需要将这些项目显示在列表视图中.帮助将是巨大的.

New to coding and i need help getting just the name of the folders in one directory. I can get the listing of the folders but it shows the full path and all i need is just the folder name.

example.

I can get this :

C:\Program Files (x86)\Adobe

but all i want is :

ADOBE

I need to get these items to show up in a list view. Help would be great. please no forwards to another site for information.

推荐答案

从.NET 4.0开始,我们有了 ^ ],使用方法EnumerateDirectoriesEnumerateDirectories(path).

来自MSDN的示例:
Since .NET 4.0 we have the DirectoryInfo class[^] with the method EnumerateDirectories and EnumerateDirectories(path).

Example from MSDN:
// Create a DirectoryInfo of the Program Files directory.
DirectoryInfo dirPrograms = new DirectoryInfo(@"c:\program files");

DateTime StartOf2009 = new DateTime(2009, 01, 01);

// LINQ query for all directories created before 2009.
var dirs = from dir in dirPrograms.EnumerateDirectories()
            where dir.CreationTimeUtc < StartOf2009
            select new
            {
                ProgDir = dir,
            };
// Show results.
foreach (var di in dirs)
{
    Console.WriteLine("{0}", di.ProgDir.Name);
}


问候,

Manfred


Regards,

Manfred


使用"\"作为分割字符分割完整目录名称,并获取字符串数组中的最后一个值.那将是您的文件夹名称

Split the full directory name using ''\'' as split character and get the last value in the string array. That will be your folder name

string [] folders=myfullPath.Split('\');
string finalFolder=folders[folders.Length-1];


这篇关于用vb.net获取文件夹名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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