如何从c#中的文件数组中读取文件内容 [英] how can u read content of file from file array in c#

查看:185
本文介绍了如何从c#中的文件数组中读取文件内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

实际上我读了文件名和大小,长度如下代码



Actually i read the file name and also size ,length as following code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.IO;
namespace ClusteringNDD
{

    public partial class Form1 : Form
    {        
        public Form1()
        {
            InitializeComponent();
        }

      
        private void button1_Click(object sender, EventArgs e)
        {
            ArrayList fileStatistics = new ArrayList();
            String datasetPath = @"D:\Data Sets\Enron";
            DirectoryInfo d = new DirectoryInfo(datasetPath);
            FileInfo[] files = d.GetFiles("*.pdf");
            MessageBox.Show(files.Length.ToString());

            foreach (FileInfo file in files)
            {                
                    //create instance of data class
                    fileAtt f = new fileAtt();
                    f.fFullName = file.FullName;
                    f.fName = file.Name;
                    f.FileSize = file.Length;
                    f.fExtension = file.Extension;
                    fileStatistics.Add(f);
                    
                
            }
         

        }
    }
}



我也尝试每个文件的foreach循环,但是有一些错误

错误如同

foreach语句不能对'System.IO类型的变量进行操作。 FileInfo'因为'System.IO.FileInfo'不包含'GetEnumerator'的公共定义

错误发生在foreach名称代码中,如


and i am also try the foreach loop for each file but some error is there
error like as
" foreach statement cannot operate on variables of type 'System.IO.FileInfo' because 'System.IO.FileInfo' does not contain a public definition for 'GetEnumerator' "
error occur in foreach name code like as

foreach(String data in file)
  {

  }





我的要求是读取文件的内容,< br $> b $ b

请帮帮我。



谢谢你



my requirement is read the content of the file,

pls help me.

thank u

推荐答案

您可以像这样读取每个文件的字节:

You could read the bytes of each file like this:
foreach (FileInfo fileInfo in files)
{
    byte[] bytes = File.ReadAllBytes(fileInfo.FullName);
}


这篇关于如何从c#中的文件数组中读取文件内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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