Read text .txt file insert data into array of string trong C# asp.net MVC

Thảo luận trong 'Lập trình web Asp.net' bắt đầu bởi seolagi, 8/9/18.

  1. seolagi
    Tham gia ngày:
    16/4/14
    Bài viết:
    1,030
    Đã được thích:
    81
    Điểm thành tích:
    48
    HI mọi người, mình có file text.txt giờ mình muốn select nó ra và insert vào 1 mảng liên tục tương ứng với 1 dòng Lines là 1 phần từ của mảng đó.
    Mã:
    StreamReader file = new StreamReader("text.txt");
    Ví dụ mình có file text.txt
    Mã:
    file@gmail.com
    file2@gmail.com
    file3@gmail.com
    Giờ mình muốn gàn hết nó vàng mảng, hoặc 1 danh sách mảng
    Mã:
    array t = {"file@gmail.com","file2@gmail.com","file3@gmail.com"};
    
    Giúp mình với, Cảm ơn
     
    Cảm ơn đã xem bài:

    Read text .txt file insert data into array of string trong C# asp.net MVC

  2. admin
    Tham gia ngày:
    22/5/13
    Bài viết:
    4,907
    Đã được thích:
    1,199
    Điểm thành tích:
    113
    Giới tính:
    Nam
    Của bạn đây:

    Cách 1:
    Mã:
    var list = new List<string>();
    using (var sr = new StreamReader("text.txt"))
    {
        string line;
        while ((line = sr.ReadLine()) != null)
        {
            list.Add(line);
        }
    }
    string[] result = list.ToArray();
    
    Cách 2:
    Mã:
    string[] result = File.ReadAllLines("text.txt");
    
    Cách 3: Xóa đi các phần từ trung trong mảng
    Mã:
    string[] result = File.ReadAllLines("text.txt");
    var Listemail = new HashSet<string>();
    Listemail = new HashSet<string>(result); //xóa đi nếu trùng lắp phần tử trong mảng
    
    Cách 4: Tạo file, ghi dữ liệu vào file
    Mã:
    //Tạo file, ghi dữ liệu vào file
    Console.WriteLine("Ban thich ghi gi trong Text???");
    String dulieunhap = Console.ReadLine();
    String filepath = "E:\\test.txt";// đường dẫn của file muốn tạo
    FileStream fs = new FileStream(filepath, FileMode.Create);//Tạo file mới tên là test.txt 
    StreamWriter sWriter = new StreamWriter(fs, Encoding.UTF8);//fs là 1 FileStream
    sWriter.WriteLine("Cộng đồng Arduino VN");
    sWriter.WriteLine(dulieunhap);
    // Ghi và đóng file
    sWriter.Flush();
    fs.Close();
    
     
    Chỉnh sửa cuối: 23/6/23
  3. seolagi
    Tham gia ngày:
    16/4/14
    Bài viết:
    1,030
    Đã được thích:
    81
    Điểm thành tích:
    48
    cảm ơn ad, cái này xài tốt : string[] result = File.ReadAllLines("text.txt");
     


Chủ để tương tự : Read text
Diễn đàn Tiêu đề Date
Lập trình web Asp.net Read text .txt file on other websites in c# MVC 23/6/23
Lập trình web Asp.net Ignore SSL Certificate DownloadString WebClient read file .txt c# MVC 23/6/23
Lập trình web Asp.net Đọc ghi nối file text .txt trong c# như thế nào? 23/6/23
Lập trình web Asp.net Xóa toàn bộ định dạng html trong chuỗi file text c# asp.net 4/5/17
Lập trình web Asp.net Nhập được thẻ html, javascript vào textbox MVC5 4/3/17