[英]how to read multiple files txt c#
嗨,我正在嘗試從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; }
}
}
發生問題是因為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();
如果要讀取同一文件夾中的多個文件並將所有內容添加到字符串列表中,請使用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());
}
在多線程環境中,您應該考慮在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());
});
並行讀取多個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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.