首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

如何获取文件夹上各个子文件夹的名称

2012-09-13 
怎么获取文件夹下各个子文件夹的名称用vb查找文件夹并移动我是刚学的vb,希望高手能指点一下。我举个例子表

怎么获取文件夹下各个子文件夹的名称
用vb查找文件夹并移动

我是刚学的vb,希望高手能指点一下。我举个例子表达我的问题吧。e盘里有个叫work的文件夹,里面有许多子文件夹,这些子文件夹的名称是按日期和类别命名的,比如有2011012189,2011012253,2011021101,2011021102,2011021201,2011021202,2011021203,……等等,前四位是年接着的两位是月再接着的两位是日,最后的几位是类别之类的,问题是我要将2月份的文件夹移动到e盘的一个新建叫201002的文件夹里,我要怎么实现呢?
我现在遇到的问题是不知道怎么读取work文件夹下的各个子文件夹的名字。

[解决办法]
枚举一个目录下的所有目录

 ' Make a reference to a directory.
Dim di As New DirectoryInfo("c:\")
' Get a reference to each directory in that directory.
Dim diArr As DirectoryInfo() = di.GetDirectories()
' Display the names of the directories.
Dim dri As DirectoryInfo
For Each dri In diArr
Console.WriteLine(dri.Name)
Next dri

参考msdn:
http://msdn.microsoft.com/en-us/library/s7xk2b58.aspx#Y900
[解决办法]
遍歷目錄下的所有目錄及文件 
Public Sub FindFile(ByVal dir1 As String)
Dim Dir2 As New DirectoryInfo(dir1)

Try
For Each d As DirectoryInfo In Dir2.GetDirectories()
FindFile(Dir2.ToString & "\" & d.ToString)

Next
For Each f As FileInfo In Dir2.GetFiles("*.*")
MessageBox.Show(f.ToString)
Next
Catch e As Exception
MessageBox.Show(e.Message)
End Try
End Sub

[解决办法]
Public Sub FindFile(dir__1 As String)
Dim Dir__2 As New DirectoryInfo(dir__1)
For Each d As DirectoryInfo In Dir__2.GetDirectories()
FindFile(Dir__2 + d.ToString() + "\")
listBox1.Items.Add(Dir__2 + d.ToString() + "\")
Next
End Sub

[解决办法]
参考msdn:http://msdn.microsoft.com/en-us/library/s7xk2b58.aspx#Y900
[解决办法]

C# code
 #region 遍历指定目录        /// <summary>        /// 遍历指定目录        /// </summary>        /// <param name="path"></param>        private void getDirectories(string path)        {            try            {                string[] fileNames = Directory.GetFiles(path);                string[] directories = Directory.GetDirectories(path);                string tempstr = "";                foreach (string file in fileNames)                {                    tempstr = file.Substring(file.LastIndexOf(@"\") + 1);                    Console.WriteLine("Filename:{0} ", tempstr);                                    }                foreach (string dir in directories)                {                    Console.WriteLine("Directoriesname:{0} ", dir);                    getDirectories(dir);                }            }            catch (Exception ce)            {                throw (ce);            }        }        #endregion 

热点排行