[英]how to read multiple files txt c#
Hi I'm trying to read 2 txt files, from an asmx web service, the reason is that in file 1 I have random letters of which I have to find matching words from file 2. But I do not know how to read the files. 嗨,我正在尝试从asmx Web服务读取2个txt文件,原因是在文件1中,我有随机字母,必须从文件2中找到匹配的单词。但是我不知道如何读取文件。
this is the webService.This is the way I am doing it. 这就是webService。这就是我的操作方式。 the idea is to read the first file and get the routes to others, which you read and add them to a list,but if you have another idea I would appreciate sharing 这个想法是读取第一个文件并获取到其他人的路由,您将其阅读并将其添加到列表中,但是如果您有其他想法,我将不胜感激
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 ".";
}
}
}
this is the controller where I upload the files and add them in an array. 这是我上载文件并将其添加到数组中的控制器。
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");
}
}
}
this is the model 这是模型
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; }
}
}
The problem occurred because File.ReadAllLines()
returns array of strings ( string[]
), you can convert it into List<string>
by using ToList()
method: 发生问题是因为File.ReadAllLines()
返回字符串数组( string[]
),您可以使用ToList()
方法将其转换为List<string>
:
string[] lines = File.ReadAllLines(@"C:\Users\thoma\source\repos\NewShoreApp\NewShoreApp\Data\CONTENIDO.txt");
List<string> list = lines.ToList();
If you want to read multiple files in the same folder and add all contents to a list of strings, use Directory.GetFiles()
or Directory.EnumerateFiles()
and iterate each file paths before using ReadAllLines()
: 如果要读取同一文件夹中的多个文件并将所有内容添加到字符串列表中,请使用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());
}
In multithreaded environment, you should consider using Parallel.ForEach
with similar setup like above over foreach
loop: 在多线程环境中,您应该考虑在foreach
循环中使用类似上述设置的Parallel.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());
});
The best way of read multiple txt files parallel is using ThreadPool. 并行读取多个txt文件的最佳方法是使用ThreadPool。
ThreadPool.QueueUserWorkItem(ReadFile, path);
and the ReadFile method is here 而ReadFile方法在这里
public static void ReadFile(Object path)
{
string content = File.ReadAllLines(@path)
// do what you need
}
If the problem is this line: 如果问题是此行:
string data= File.ReadAllLines(line); //'Cannot implicitly convert type string[] to string'
The variable lines is an array of each line as a string, that you already called above. 可变行是每行的数组,是一个字符串,您已经在上面调用了它。
Just cast the array of lines to a list if you want a list of lines: 如果需要行列表,只需将行数组转换为列表即可:
var list = new List<string>(data);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.