如何读取多个文件txt c#

问题描述 投票:1回答:3

嗨我正在尝试从asmx web服务中读取2个txt文件,原因是在文件1中我有随机字母,我必须从文件2中找到匹配的单词。但我不知道如何读取文件。

这是webService。这就是我这样做的方式。我的想法是读取第一个文件并获取其他人的路线,您将其读取并添加到列表中,但如果您有其他想法,我会很感激分享

namespace NewShoreApp
{        
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]

    public class WebService : System.Web.Services.WebService
    {

        [WebMethod]
        public string ReadData()
        {

            string[] lines = File.ReadAllLines(@"C:\Users\thoma\source\repos\NewShoreApp\NewShoreApp\Data\CONTENIDO.txt");

            List<string> list = new List<string>();

            foreach (var line in lines)
            {

                string data= File.ReadAllLines(line); //'Cannot implicitly convert type string[] to string'

                list.AddRange(data); //Cannot convert from string to system.collections.generic IEnumerable<string>
            }

                return ".";                        
            }
    }
}

这是我上传文件并将它们添加到数组中的控制器。

namespace NewShoreApp.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {

            return View();
        }


        [HttpPost]
        public ActionResult Index(HttpPostedFileBase[] files)
        {

            if (ModelState.IsValid)
            {
                try
                {
                    foreach (HttpPostedFileBase file in files)
                    {
                        if (file != null)
                        {
                            var ServerPath = Path.Combine(Server.MapPath("~/Data"), Path.GetFileName(file.FileName));

                            file.SaveAs(ServerPath);
                        }
                    }                    
                    ViewBag.FileStatus = "File uploaded successfully.";
                }

                catch (Exception)   
                {

                    ViewBag.FileStatus = "Error while file uploading.";
                }

            }
            return View("Index");
        }

    }
}

这是模型

namespace NewShoreApp.Models
{
    public class Data
    {
        // 
        [DataType(DataType.Upload)]
        [Display(Name = "Upload File")]
        [Required(ErrorMessage = "Please choose file to upload.")]
        public HttpPostedFileBase[] files { get; set; }

    }
}
c# asp.net asp.net-mvc web-services asmx
3个回答
3
投票

出现问题的原因是File.ReadAllLines()返回字符串数组(string[]),您可以使用List<string>方法将其转换为ToList()

string[] lines = File.ReadAllLines(@"C:\Users\thoma\source\repos\NewShoreApp\NewShoreApp\Data\CONTENIDO.txt");

List<string> list = lines.ToList();

如果要读取同一文件夹中的多个文件并将所有内容添加到字符串列表中,请使用Directory.GetFiles()Directory.EnumerateFiles()并在使用ReadAllLines()之前迭代每个文件路径:

List<string> paths = Directory.EnumerateFiles(@"C:\Users\thoma\source\repos\NewShoreApp\NewShoreApp\Data\", "*.txt").ToList();

foreach (string filePath in paths)
{
    string[] lines = File.ReadAllLines(filePath);

    list.AddRange(lines.ToList());
}

在多线程环境中,你应该考虑在Parallel.ForEach循环中使用foreach和上面类似的设置:

List<string> paths = Directory.EnumerateFiles(@"C:\Users\thoma\source\repos\NewShoreApp\NewShoreApp\Data\", "*.txt").ToList();
Parallel.ForEach(paths, current => 
{
    string[] lines = File.ReadAllLines(current);

    list.AddRange(lines.ToList());
});

2
投票

读取多个txt文件并行的最佳方法是使用ThreadPool。

ThreadPool.QueueUserWorkItem(ReadFile, path);

而ReadFile方法就在这里

public static void ReadFile(Object path)
{
 string content = File.ReadAllLines(@path)
 // do what you need 
}

2
投票

如果问题是这一行:

string data= File.ReadAllLines(line); //'Cannot implicitly convert type string[] to string'

变量行是每行的数组,作为字符串,您已在上面调用过。

如果您想要一个行列表,只需将行数组转换为列表:

var list = new List<string>(data); 
© www.soinside.com 2019 - 2024. All rights reserved.