C# 如何读取txt c中的多个文件#
您好,我正在尝试从asmx web服务读取2个txt文件,原因是在文件1中我有随机字母,我必须从文件2中找到匹配的单词。但我不知道如何阅读这些文件 这是网络服务。这是我做这件事的方式。我们的想法是阅读第一个文件并获得到其他人的路由,您阅读并将其添加到列表中,但是如果您有其他想法,我将非常感谢与大家分享C# 如何读取txt c中的多个文件#,c#,asp.net,asp.net-mvc,web-services,asmx,C#,Asp.net,Asp.net Mvc,Web Services,Asmx,您好,我正在尝试从asmx web服务读取2个txt文件,原因是在文件1中我有随机字母,我必须从文件2中找到匹配的单词。但我不知道如何阅读这些文件 这是网络服务。这是我做这件事的方式。我们的想法是阅读第一个文件并获得到其他人的路由,您阅读并将其添加到列表中,但是如果您有其他想法,我将非常感谢与大家分享 namespace NewShoreApp { [WebService(Namespace = "http://tempuri.org/")] [WebServic
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.Models
{
public class Data
{
//
[DataType(DataType.Upload)]
[Display(Name = "Upload File")]
[Required(ErrorMessage = "Please choose file to upload.")]
public HttpPostedFileBase[] files { get; set; }
}
}
并行读取多个txt文件的最佳方法是使用ThreadPool
ThreadPool.QueueUserWorkItem(ReadFile, path);
ReadFile方法在这里
public static void ReadFile(Object path)
{
string content = File.ReadAllLines(@path)
// do what you need
}
并行读取多个txt文件的最佳方法是使用ThreadPool
ThreadPool.QueueUserWorkItem(ReadFile, path);
ReadFile方法在这里
public static void ReadFile(Object path)
{
string content = File.ReadAllLines(@path)
// do what you need
}
如果此线路存在问题:
string data= File.ReadAllLines(line); //'Cannot implicitly convert type string[] to string'
变量行是每个行作为字符串的数组,您已经在上面调用了它
如果需要行列表,只需将行数组强制转换为列表:
var list = new List<string>(data);
var列表=新列表(数据);
如果问题出在这一行:
string data= File.ReadAllLines(line); //'Cannot implicitly convert type string[] to string'
变量行是每个行作为字符串的数组,您已经在上面调用了它
如果需要行列表,只需将行数组强制转换为列表:
var list = new List<string>(data);
var列表=新列表(数据);
出现问题的原因是返回字符串数组(string[]
),您可以使用ToList()
方法将其转换为List
:
string[] lines = File.ReadAllLines(@"C:\Users\thoma\source\repos\NewShoreApp\NewShoreApp\Data\CONTENIDO.txt");
List<string> list = lines.ToList();
在多线程环境中,您应该考虑使用<代码>并行.Frace >类似的设置,如上面的代码>前缀循环:
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());
});
List path=Directory.EnumerateFiles(@“C:\Users\thoma\source\repos\NewShoreApp\NewShoreApp\Data\,“*.txt”).ToList();
Parallel.ForEach(路径,当前=>
{
string[]lines=File.ReadAllLines(当前);
list.AddRange(lines.ToList());
});
出现问题的原因是返回字符串数组(string[]
),您可以使用ToList()
方法将其转换为List
:
string[] lines = File.ReadAllLines(@"C:\Users\thoma\source\repos\NewShoreApp\NewShoreApp\Data\CONTENIDO.txt");
List<string> list = lines.ToList();
在多线程环境中,您应该考虑使用<代码>并行.Frace >类似的设置,如上面的代码>前缀循环:
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());
});
List path=Directory.EnumerateFiles(@“C:\Users\thoma\source\repos\NewShoreApp\NewShoreApp\Data\,“*.txt”).ToList();
Parallel.ForEach(路径,当前=>
{
string[]lines=File.ReadAllLines(当前);
list.AddRange(lines.ToList());
});
非常感谢您的推荐非常感谢您的推荐非常感谢您的回答,当我使用“Directory.EnumerateFiles”时,我发现错误无法将type“System.Collections.Generic.IEnumerable”隐式转换为string[]好吧,我实现了IEnumerable
和string[]
有不同的类型。我建议使用var
或ToList()
而不是显式使用string[]
。非常感谢您的回答,当我使用“Directory.EnumerateFiles”时,我发现错误无法隐式地将type“System.Collections.Generic.IEnumerable”转换为string[],我意识到IEnumerable
和string[]
有不同的类型。我建议使用var
或ToList()
而不是显式使用string[]
。我想投u票,但需要问题版。我想投u票,但需要问题版。