第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

如何讀取多個(gè)文件txt c#

如何讀取多個(gè)文件txt c#

C#
交互式愛(ài)情 2022-10-23 16:29:32
我正在嘗試從 asmx Web 服務(wù)讀取 2 個(gè) txt 文件,原因是在文件 1 中我有隨機(jī)字母,我必須從文件 2 中找到匹配的單詞。但我不知道如何讀取文件.這是 webService。這就是我的做法。這個(gè)想法是讀取第一個(gè)文件并獲取其他人的路線,您閱讀并將它們添加到列表中,但如果您有其他想法,我將不勝感激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 ".";                                    }    }}這是我上傳文件并將它們添加到數(shù)組中的控制器。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.";                }            }        }    }}
查看完整描述

3 回答

?
躍然一笑

TA貢獻(xiàn)1826條經(jīng)驗(yàn) 獲得超6個(gè)贊

出現(xiàn)問(wèn)題是因?yàn)镕ile.ReadAllLines()返回字符串?dāng)?shù)組( ),您可以使用方法string[]將其轉(zhuǎn)換為:List<string>ToList()


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


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

如果要讀取同一文件夾中的多個(gè)文件并將所有內(nèi)容添加到字符串列表中,請(qǐng)使用Directory.GetFiles()orDirectory.EnumerateFiles()并在使用之前迭代每個(gè)文件路徑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());

}

在多線程環(huán)境中,您應(yīng)該考慮使用Parallel.ForEach與上述類似的設(shè)置foreach循環(huán):


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());

});


查看完整回答
反對(duì) 回復(fù) 2022-10-23
?
泛舟湖上清波郎朗

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超3個(gè)贊

并行讀取多個(gè) txt 文件的最佳方法是使用 ThreadPool。


ThreadPool.QueueUserWorkItem(ReadFile, path);

ReadFile 方法在這里


public static void ReadFile(Object path)

{

 string content = File.ReadAllLines(@path)

 // do what you need 

}


查看完整回答
反對(duì) 回復(fù) 2022-10-23
?
海綿寶寶撒

TA貢獻(xiàn)1809條經(jīng)驗(yàn) 獲得超8個(gè)贊

如果問(wèn)題是這一行:

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

變量lines 是每行作為字符串的數(shù)組,您已經(jīng)在上面調(diào)用過(guò)。

如果您想要行列表,只需將行數(shù)組轉(zhuǎn)換為列表:

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


查看完整回答
反對(duì) 回復(fù) 2022-10-23
  • 3 回答
  • 0 關(guān)注
  • 252 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)