Đọc ghi file .txt trong c# với visual studio 2010

Thảo luận trong 'Lập trình phần mềm' bắt đầu bởi admin, 8/9/14.

  1. admin
    Tham gia ngày:
    22/5/13
    Bài viết:
    4,894
    Đã được thích:
    1,198
    Điểm thành tích:
    113
    Giới tính:
    Nam
    Code đọc dữ liệu từ file .txt trong c#.
    Code ghi dữ liệu vào file .txt c#.
    Đây là toàn bộ code đọc ghi file .txt trong c# sử dụng visual studio

    Mã:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Collections.Specialized;
    
    namespace CheckIP
    {
        public partial class CheckIP : Form
        {
            private OpenFileDialog FileOpen = new OpenFileDialog();
            private string chooseFile;
            public CheckIP()
            {
              
                InitializeComponent();
                this.MaximizeBox = false;
                this.StartPosition = FormStartPosition.CenterScreen;
                FileOpen.FileOk += new CancelEventHandler(FileOpen_FileOk);
            }
    
            void FileOpen_FileOk(object sender, CancelEventArgs e)
            {
                chooseFile = FileOpen.FileName;
                this.Text = Path.GetFileName(chooseFile);
                DisplayFile();
            }
    
            private void CheckIP_Load(object sender, EventArgs e)
            {
                FileOpen.ShowDialog(); // mở file
            }
     // đọc file start    
            private void DisplayFile()
            {
                int nCols = 16;
                FileStream inStream = new FileStream(chooseFile, FileMode.Open,
                                                                 FileAccess.Read);
                long nBytesToRead = inStream.Length;
                if (nBytesToRead > 65536 / 4)
                    nBytesToRead = 65536 / 4;
                int nLines = (int)(nBytesToRead / nCols) + 1;
                string[] lines = new string[nLines];
                int nBytesRead = 0;
                for (int i = 0; i < nLines; i++)
                {
                    StringBuilder nextLine = new StringBuilder();
                    nextLine.Capacity = 4 * nCols;
                    for (int j = 0; j < nCols; j++)
                    {
                        int nextByte = inStream.ReadByte();
                        nBytesRead++;
                        if (nextByte < 0 || nBytesRead > 65536)
                            break;
                        char nextChar = (char)nextByte;
                        if (nextChar < 16)
                            nextLine.Append(" x0" + string.Format("{0,1:X}",
                                                                 (int)nextChar));
                        else if (char.IsLetterOrDigit(nextChar) ||
                                                 char.IsPunctuation(nextChar))
                            nextLine.Append(" " + nextChar + " ");
                        else nextLine.Append(" x" + string.Format("{0,2:X}",
                                                   (int)nextChar));
                    }
                    lines[i] = nextLine.ToString();
                }
    
                inStream.Close();
    
                richTextBox.Lines = lines;
            }
    // đọc file end
    // ghi file start
            void SaveFile()
            {
                StreamWriter sw = new StreamWriter(chooseFile, false,
                                      Encoding.Unicode);
                foreach (string line in richTextBox.Lines)
                    sw.WriteLine(line);
                sw.Close();
            }
    // ghi file end
            
        }
    }
    
    
    
    
    Demo

    Download code trên từ link mediafire về.
    Code được viết trên visual 10, dnet 4.0
     
    Cảm ơn đã xem bài:

    Đọc ghi file .txt trong c# với visual studio 2010

  2. nhadatpro
    Tham gia ngày:
    12/3/18
    Bài viết:
    27
    Đã được thích:
    0
    Điểm thành tích:
    6
    Giới tính:
    Nam
    upppppppppppppppppppppppppppp
     


Chủ để tương tự : Đọc file
Diễn đàn Tiêu đề Date
Lập trình phần mềm Code c# đọc danh sách file trong folder(thư mục) 9/9/14
Lập trình phần mềm Đọc, ghi, xóa dữ liệu XML trong c#, asp.net 30/1/16
Lập trình phần mềm Đọc, ghi, xóa (thêm, xóa, sửa) dữ liệu trong tập tin XML trong lập trình c# 2/3/14
Lập trình phần mềm Code c# lấy filename, duôi file, getExtension, getFileName 9/9/14
Lập trình phần mềm Save file PowerPoint dưới dạng web(html,htm) trên PowerPoint 2010 17/9/13