C# 循环遍历文件集,并检查它是否在C中以逗号分隔#
我需要遍历一组文件,并检查它是否在C#中以逗号分隔。我对C#非常陌生。请帮我解决这个问题C# 循环遍历文件集,并检查它是否在C中以逗号分隔#,c#,ssis,C#,Ssis,我需要遍历一组文件,并检查它是否在C#中以逗号分隔。我对C#非常陌生。请帮我解决这个问题 提前感谢。正如有人指出的,这不是一个容易的解决方案。 如果每个以逗号分隔的文件都有一个指定的扩展名(例如:csv),那么就很容易了。如果不是,则以下算法应起作用: 检索指定目录中文件的所有名称(路径+名称)。如果需要,只过滤那些可能感兴趣的内容。提示:查看System.IO.Directory和System.IO.File和System.IO.DirectoryInfo和System.IO.FileInfo
提前感谢。正如有人指出的,这不是一个容易的解决方案。 如果每个以逗号分隔的文件都有一个指定的扩展名(例如:csv),那么就很容易了。如果不是,则以下算法应起作用:
System.IO.Directory
和System.IO.File
和System.IO.DirectoryInfo
和System.IO.FileInfo
正则表达式一开始有点难学,但一段时间后就会有回报 这是一个快速控制台应用程序,它将获取一个目录,扫描该目录中的所有文件,然后遍历这些文件,并返回包含逗号的行的百分比与文件中的总行的百分比。正如已经指出的,您可以根据CSV库进行验证。这只是一个快速的例子,让你开始 要使用它,请在VisualStudio中创建一个新的控制台应用程序项目,并将其命名为“TestStub”,然后将其复制并传递到“Program.cs”文件中
名称空间测试存根
{
使用制度;
使用System.IO;
使用系统文本;
公共课程
{
私有静态字符[]CSV={',',',',};
私有静态bool csvFound=false;
///
///这是控制台程序入口点
///
///启动时传递到此应用程序的任何命令行参数的列表
公共静态void Main(字符串[]args)
{
//如果愿意,请将此更改为使用args[0]
字符串myInitialPath=@“C:\Temp”;
字符串[]myListOfFiles;
尝试
{
myListOfFiles=枚举文件(myInitialPath);
foreach(myListOfFiles中的字符串文件)
{
Console.WriteLine(“\n文件{0}由{1}%CSV分隔行组成。”,
文件
ScanForCSV(文件));
}
Console.WriteLine(“\n\n按任意键退出”);
Console.ReadKey();
}
捕获(例外情况除外)
{
控制台写入线(
“处理CSV内容{1}:{2}的{0}时出错”,
我的初始路径,
例如,信息,
例如InnerException.Message);
}
}
///
///获取指定路径的所有文件的列表
///
///目录路径
///文件的字符串数组(具有完整路径)
公共静态字符串[]枚举文件(字符串路径)
{
string[]arrItems=新字符串[1];
尝试
{
arrpitems=Directory.GetFiles(路径);
归还物品;
}
捕获(例外情况除外)
{
抛出新System.IO.IOException(“EnumerateFilesAndFolders()遇到错误:”,ex);
}
}
///
///确定提供的文件是否具有逗号分隔的值
///
///路径和文件名
///包含CSV元素的行的百分比-与不包含CSV元素的行的百分比
公共静态浮点扫描CSV(字符串文件名)
{
//
//注意:您应该查看许多CSV库中的一个
//可用。此方法不会仔细检查
//查看文件中是否有delimeters或delimeters的组合
//即使是纯文本(如报纸文章)
//它只是查找多行中是否存在逗号
//并计算其中有无的百分比
//
浮点数=0;
浮动线SCSV=0;
尝试
{
使用(StreamReader sReader=newstreamreader(文件名))
{
int元素=0;
string line=string.Empty;
字符串[]已解析=新字符串[1];
而(!sReader.EndOfStream)
{
++总数;
line=sReader.ReadLine();
解析=行分割(CSV);
elements=parsed.Length;
如果(元素>1)
{
++linesCSV;
}
}
}
}
捕获(例外情况除外)
{
抛出新的System.IO.IOException(string.Format(“访问[{0}]的问题]:{1}”,文件名,ex.Message),ex);
}
返回(浮动)((行SCSV/总计)*100);
}
}
}
}你试过什么?您如何知道文件是否以逗号分隔?文本中有一个或多个逗号就足够了吗?为什么不使用许多可用的CSV库中的一个,看看文件是否有效?您已经标记了SSI,是否正在尝试在SSI中使用C#?您必须使用Cා或您的根问题,您需要验证SSIS中的文件吗?CSV(N列)是否有预期的格式,或者任何带有逗号的格式都可以吗?尝试用更多细节更新您的问题,否则问题将被关闭。
namespace TestStub
{
using System;
using System.IO;
using System.Text;
public class Program
{
private static char[] CSV = { ',', ',' };
private static bool csvFound = false;
/// <summary>
/// This is the console program entry point
/// </summary>
/// <param name="args">A list of any command-line args passed to this application when started</param>
public static void Main(string[] args)
{
// Change this to use args[0] if you like
string myInitialPath = @"C:\Temp";
string[] myListOfFiles;
try
{
myListOfFiles = EnumerateFiles(myInitialPath);
foreach (string file in myListOfFiles)
{
Console.WriteLine("\nFile {0} is comprised of {1}% CSV delimited lines.",
file,
ScanForCSV(file));
}
Console.WriteLine("\n\nPress any key to exit.");
Console.ReadKey();
}
catch (Exception ex)
{
Console.WriteLine(
"Error processing {0} for CSV content: {1} :: {2}",
myInitialPath,
ex.Message,
ex.InnerException.Message);
}
}
/// <summary>
/// Get a list of all files for the specified path
/// </summary>
/// <param name="path">Directory path</param>
/// <returns>String array of files (with full path)</returns>
public static string[] EnumerateFiles(string path)
{
string[] arrItems = new string[1];
try
{
arrItems = Directory.GetFiles(path);
return arrItems;
}
catch (Exception ex)
{
throw new System.IO.IOException("EnumerateFilesAndFolders() encountered an error:", ex);
}
}
/// <summary>
/// Determines if the supplied file has comma separated values
/// </summary>
/// <param name="filename">Path and filename</param>
/// <returns>Percentage of lines containing CSV elements -vs- those without</returns>
public static float ScanForCSV(string filename)
{
//
// NOTE: You should look into one of the many CSV libraries
// available. This method will not carefully scruitinize
// the file to see if there's a combination of delimeters or
// even if it's a plain-text (e.g. a newspaper article)
// It just looks for the presence of commas on multiple lines
// and calculates a percentage of them with and without
//
float totalLines = 0;
float linesCSV = 0;
try
{
using (StreamReader sReader = new StreamReader(filename))
{
int elements = 0;
string line = string.Empty;
string[] parsed = new string[1];
while (!sReader.EndOfStream)
{
++totalLines;
line = sReader.ReadLine();
parsed = line.Split(CSV);
elements = parsed.Length;
if (elements > 1)
{
++linesCSV;
}
}
}
}
catch (Exception ex)
{
throw new System.IO.IOException(string.Format("Problem accessing [{0}]: {1}", filename, ex.Message), ex);
}
return (float)((linesCSV / totalLines) * 100);
}
}
}