读取本地文件夹文件使用控制台应用程序导出为CSV [英] Read Local Folder Files Export to CSV using Console Application

查看:86
本文介绍了读取本地文件夹文件使用控制台应用程序导出为CSV的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好

我有子文件夹文件的本地文件夹读取文件并使用控制台应用程序将这些文件导出为CSV



  static   void  Main( string  [] args)
{

string [] Filepath = Directory.GetFiles( @ E:\ _1150 \ 1150 *。*,SearchOption.AllDirectories);
// 转换流字符串
StreamReader streamReader = new StreamReader(filePath);
string text = streamReader.ReadToEnd();
streamReader.Close();


}
}
静态 void GetFiles( string dir)
{
try
{
foreach string f in Directory.GetFiles(dir))
Console.WriteLine(f);
foreach string d in Directory.GetDirectories(dir))
{
Console.WriteLine(d);
GetFiles(d);
}
}
catch (System.Exception ex)
{
Console.WriteLine(ex。信息);
}

}
}
}





注意:这里Directory.GetFiles类读取所有.txt文件但是Stream读取器类读取一个文件我需要读取所有文件并将文件导出到csv



请你能帮助我吗? />
thankyou

解决方案

获取文本文件列表并处理每个文件如下

  string  [] files = 
Directory.GetFiles( @ E:\ _1150 \ _1150 \ * .txt ,SearchOption.AllDirectories);
foreach 字符串文件 in 文件)
{
使用(StreamReader streamReader = new StreamReader(file) )
{
string text = streamReader.ReadToEnd();
// 对文件内容执行某些操作
}
}



引用:

我们在该测试文件中创建了一个test.csv文件2行

1. folderName和

2. nameofFile



string [] files包含文件路径<的数组/ b>(目录和文件名都在字符串中),阅读路径。 GetFileName方法 [ ^ ]和 Path.GetDirectoryName方法 [ ^ ]这将帮助您获取文件名和目录f orm路径字符串。



现在我们来到了Final Task;

编写csv很简单,例如

 使用 var  w =  new  StreamWriter(路径)) //   csv文件的路径 
{
foreach 字符串 filepath 文件中) // 循环每个文件路径
{
var directory = Path.GetDirectoryName(file);
var fileName = Path.GetFileName(file);
var line = string .Format( {0},{1},directory,fileName);
w.WriteLine(line);
w.Flush();
}
}



希望这有助于你


DamithSL的解决方案1做了什么你问 - 它读取文件名和路径并创建一个csv文件。

如果你正在寻找与csv文件不同的东西;一个很好的库是FileHelpers - 做一个搜索,你会发现它。


Hi All
I have local folder with sub-folder files read that Files and Export those files into CSV using Console Application

static void Main(string[] args)
        {

            string[] Filepath = Directory.GetFiles(@"E:\1150\1150", "*.*", SearchOption.AllDirectories);
            // converting Stream string
               StreamReader streamReader = new StreamReader(filePath);
                string text = streamReader.ReadToEnd();
               streamReader.Close();


           }
        }
        static void GetFiles(string dir)
        {
            try
            {
                foreach (string f in Directory.GetFiles(dir))
                    Console.WriteLine(f);
                foreach (string d in Directory.GetDirectories(dir))
                {
                    Console.WriteLine(d);
                    GetFiles(d);
                }
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

        }
    }
}



Note : Here Directory.GetFiles class Reading All .txt files But Stream Reader Class Reading one File i need to Read all Files and Export files into csv

Please can u Help me
thankyou

解决方案

get the list of text files and process each file as below

string[] files =
            Directory.GetFiles(@"E:\1150\1150\","*.txt", SearchOption.AllDirectories);
foreach(string file in files )
{
    using(StreamReader streamReader = new StreamReader(file))
    {
      string text = streamReader.ReadToEnd();
      // do something with file content
    }
}


Quote:

we have create one test.csv file in that test file we have 2 rows
1. folderName and
2. nameofFile


string[] files contains array of File Paths (both directory and filename is in the string), read Path.GetFileName Method[^] and Path.GetDirectoryName Method[^] which will help you to get filename and Directory form path string.

Now We came to Final Task;
writing csv is simple, for example

using(var w = new StreamWriter(path))// path of csv file
{
    foreach(string filepath in files ) // loop each file path 
    {
        var directory= Path.GetDirectoryName(file);
        var fileName= Path.GetFileName(file);
        var line = string.Format("{0},{1}", directory, fileName);
        w.WriteLine(line);
        w.Flush();
    }
}


hope this helps you


Solution 1 from DamithSL does what you ask - it reads filename and path and creates a csv file .
If you are looking for more than that to do with csv files ;one nice library is FileHelpers - do a search and you will find it.


这篇关于读取本地文件夹文件使用控制台应用程序导出为CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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