[置顶] c#对于文件的操作

在system.iO;命名空间中提供了两种遍历枚举文件夹的类,Directory 和DirectoryInfo,

他们都有一个方法GetDirectories();来便利枚举文件夹,

        /// <summary>
/// 遍历根目录 ,获取专业名称
/// </summary>
/// <returns></returns>
public DataTable GetYearName(string path)
{
//string strYear = "";
DataTable tbYear = new DataTable("年份表");
string[] arrPath = Directory.GetDirectories(path);
//添加表头
tbYear.Columns.Add("id");
tbYear.Columns.Add("year_url");
tbYear.Columns.Add("year_name");
for (int i = 0; i < arrPath.Length / 2; i++)
{
string temp = arrPath[i];
arrPath[i] = arrPath[arrPath.Length - 1 - i];
arrPath[arrPath.Length - 1 - i] = temp;
}
for (int i = 0; i < arrPath.Length; i++)
{ string[] arrSplit = arrPath[i].ToString().Split('\\');
tbYear.Rows.Add(new object[] { i, arrPath[i], arrSplit[arrSplit.Length - 1] }); }
return tbYear;
}
/// <summary>
/// 遍历根目录 ,获取专业名称
/// </summary>
/// <returns></returns>
public DataTable GetMajorName(string path)
{
DataTable tbMajor = new DataTable("专业表");
string[] arrPath = Directory.GetDirectories(path);
//添加表头
tbMajor.Columns.Add("major_id");
tbMajor.Columns.Add("major_url");
tbMajor.Columns.Add("major_name");
for (int i = 0; i < arrPath.Length; i++)
{
string[] arrSplit = arrPath[i].ToString().Split('\\');
tbMajor.Rows.Add(new object[] { i, arrPath[i], arrSplit[arrSplit.Length - 1] }); }
return tbMajor;
//string strMajor = "";
//DataTable tbMajor = new DataTable("专业表");
//tbMajor.Columns.Add("major_name");
//DirectoryInfo theDepInfo = new DirectoryInfo(path);
//foreach (DirectoryInfo folder in theDepInfo.GetDirectories())
//{
// strMajor += folder.Name + ",";
// tbMajor.Rows.Add(folder.Name);
//}
//return tbMajor;
}
/// <summary>
/// 获取专业文件夹下班级名称
/// </summary>
/// <param name="majorName">专业名称</param>
/// <returns></returns>
public DataTable GetClassName(string path)
{
DataTable tbClasses = new DataTable("班级表");
string[] arrPath = Directory.GetDirectories(path);
//添加表头
tbClasses.Columns.Add("class_id");
tbClasses.Columns.Add("class_url");
tbClasses.Columns.Add("class_name");
for (int i = 0; i < arrPath.Length; i++)
{
string[] arrSplit = arrPath[i].ToString().Split('\\');
tbClasses.Rows.Add(new object[] { i, arrPath[i], arrSplit[arrSplit.Length - 1] }); }
return tbClasses;
}
/// <summary>
/// 获取班级文件夹下班级名称
/// </summary>
/// <param name="majorName">专业名称</param>
/// <returns></returns>
public DataTable GetStudentName(string path)
{ DataTable tbStudents = new DataTable("学生信息表"); string[] arrPath = Directory.GetDirectories(path);
//添加表头
tbStudents.Columns.Add("_id");
tbStudents.Columns.Add("student_url");
tbStudents.Columns.Add("name");
tbStudents.Columns.Add("student_num");
tbStudents.Columns.Add("student_name");
tbStudents.Columns.Add("teacher");
tbStudents.Columns.Add("select_topic");
for (int i = 0; i < arrPath.Length; i++)
{
string[] arrSplit = arrPath[i].ToString().Split('\\');
string name = arrSplit[arrSplit.Length - 1];
if(name.Length<=0)
{
continue;
}
string []arrInfo=name.Split(new char[]{'_','-'});
if (arrInfo.Length < 4)
{
continue;
}
tbStudents.Rows.Add(new object[] { i, arrPath[i], name,arrInfo[0],arrInfo[1],arrInfo[2],arrInfo[3]});
}
return tbStudents; }

下面是使用了foreach,上面的看起来比较笨,但是专业性会更好一点

 /// <summary>
/// 获取专业文件夹下班级名称
/// </summary>
/// <param name="majorName">专业名称</param>
/// <returns></returns>
public DataTable GetClasses(string path,string majorName)
{
string strClasses = "";
DataTable tbClasses = new DataTable("班级表");
tbClasses.Columns.Add("class_name");
DirectoryInfo theDepInfo = new DirectoryInfo(rootDirectoryUrl);
foreach (DirectoryInfo folder in theDepInfo.GetDirectories())
{
strClasses += folder.Name + ",";
tbClasses.Rows.Add(new object[] { folder.Name }); }
return tbClasses;
}
上一篇:.Net Core 在 Linux-Centos上的部署实战教程(三)


下一篇:Android Studio 初级安装