C#读取文件目录输出到CSV文件中

土豆你个番茄 土豆你个番茄 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Data;
using Microsoft.VisualStudio.SourceSafe.Interop;


class DirectoryFileCount
{

static long files = 0;
static long directories = 0;


static void Main()
{
try
{
Console.WriteLine("Enter the path to a directory:");

string directory = Console.ReadLine();

// Create a new DirectoryInfo object.
DirectoryInfo dir = new DirectoryInfo("C:/123");

if (!dir.Exists)
{
throw new DirectoryNotFoundException("The directory does not exist.");
}

// Call the GetFileSystemInfos method.
FileSystemInfo[] infos = dir.GetFileSystemInfos();

//Console.WriteLine("Working...");

// Pass the result to the ListDirectoriesAndFiles
// method defined below.
ListDirectoriesAndFiles(infos);

// Display the results to the console.
Console.WriteLine("directories: {0}", directories);
Console.WriteLine("files: {0}", files);

}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
finally
{

Console.ReadLine();
}
}

static void ListDirectoriesAndFiles(FileSystemInfo[] FSInfo)
{
// Check the FSInfo parameter.
if (FSInfo == null)
{
throw new ArgumentNullException("FSInfo");
}


List<string> list = new List<string>();
// Iterate through each item.
foreach (FileSystemInfo i in FSInfo)
{
// Check to see if this is a DirectoryInfo object.
if (i is DirectoryInfo)
{
// Add one to the directory count.
directories++;

// Cast the object to a DirectoryInfo object.
DirectoryInfo dInfo = (DirectoryInfo)i;

// Iterate through all sub-directories.
ListDirectoriesAndFiles(dInfo.GetFileSystemInfos());




Console.WriteLine(dInfo.FullName);

list.Add(dInfo.FullName);



// 实例化一个StreamWriter 对象,创建文件
using (StreamWriter sw = new StreamWriter(@"C:\new.txt"))
{
foreach (string para in list)
{
//输出信息
//Console.WriteLine(para);
//写入信息
sw.WriteLine(para);
sw.Flush();
}
}



}
// Check to see if this is a FileInfo object.
else if (i is FileInfo)
{
// Add one to the file count.
files++;

FileInfo fInfo = (FileInfo)i;

//Console.WriteLine("**************所有文件******************");


// Console.WriteLine(fInfo.Name);



}

}
}
}





谁帮我看一下,为什么我不能输出全部的控制台信息,只能输出一部分
...全文
评论 4
收藏 已收藏
分享
返回顶部
土豆你个番茄
C# 111,128 成员 642,537 内容