[C Chen] 201801013 operation of documents

1. The reading and writing of stream based text files is relatively simple for C ා. Generally speaking, the following five basic steps are required:

  1. Create a file stream -- FileStream
  2. Create reader or writer - StreamReader/StreamWriter
  3. Perform read or write operations
  4. Close reader or writer
  5. Close file stream

2. BinaryReader and BinaryWriter classes can be used to read and write binary files
These two classes are not derived from Stream, but directly from System.Object class
All objects that create two classes must be based on the provided objects derived from the Stream class, such as the FileStream class

3. File class and FileInfo class -- basic file operation
Directory class and DirectoryInfo class -- directory basic operation
Path class and environment class -- path class and system information class

4. When the file to be read contains Chinese characters, you need to specify the code Encoding.GetEncoding("GB2312")
string[ ] content=File.ReadAllLines(path,Encoding.GetEncoding("GB2312"));

5. Read and write text files

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Text;

namespace WindowsFormsApp4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void btnRead_Click(object sender, EventArgs e)
        {
            if (ofdOpen.ShowDialog() == DialogResult.OK)
            {
                string filePath = ofdOpen.FileName;
                txtFilePath.Text = filePath;

                if (File.Exists(filePath))
                {
                    StreamReader sr = new StreamReader(filePath, Encoding.GetEncoding("gb2312"));
                    string input;
                    while ((input = sr.ReadLine()) != null)
                    {
                        txtFileContent.Text += input + "\r\n";
                    }
                    sr.Close();
                }
                else
                {
                    MessageBox.Show("The file you want to read does not exist!");
                }
            }
        }

        private void btnWrite_Click(object sender, EventArgs e)
        {
            sfdSave.Filter = "text file(*.txt)|*.txt";
            if (sfdSave.ShowDialog() == DialogResult.OK)
            {
                string filePath = sfdSave.FileName;
                StreamWriter sw = new StreamWriter(filePath, false);
                sw.WriteLine(txtFileContent.Text);
                sw.Close();
            }
        }
    }
}

6. Basic operation of documents

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using Microsoft.VisualBasic.Devices;

namespace WindowsFormsApp3
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            string path = @txtFile.Text;
            if (File.Exists(path))
                MessageBox.Show("File name already exists");
            try
            {
                using(StreamWriter sw = File.CreateText(path))
                {
                    sw.WriteLine("Don't want to talk");
                    sw.WriteLine("");
                    sw.WriteLine("Establishment date time"+DateTime.Now.ToString());
                    sw.Flush();
                    MessageBox.Show("File created successfully!");
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show("The file could not be created." + Environment.NewLine + "Please confirm whether the file name is correct," + "And whether you have permission to create.");
            }
        }

        private void btnCopy_Click(object sender, EventArgs e)
        {
            try
            {
                File.Copy(@"d:\t87.txt", @"d:\t87_1.txt", true);
                FileInfo fi = new FileInfo(@"d:\t87.txt");
                fi.CopyTo(@"d:\t87_2.txt", true);
                MessageBox.Show("File copied");
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

        private void btnRename_Click(object sender, EventArgs e)
        {
            try
            {
                Computer MyComputer = new Computer();
               // File.Copy(@"d:\t87.text", @"d:\t87_3.text", true);
                MyComputer.FileSystem.RenameFile(@"d:\t87.txt", "Change file name.txt");
                MessageBox.Show("Change file name succeeded!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }

        private void btnMove_Click(object sender, EventArgs e)
        {
           if(File.Exists(@"d:\t87.txt")){

                //The file is occupied by a thread and cannot be moved. The solution is to recopy a move
                File.Copy(@"d:\t87.txt", @"d:\move.txt",true);
                File.Move(@"d:\move.txt", @"d:\Mobile 1.txt");
                MessageBox.Show("File move succeeded!");
            }
            else
            {
                FileInfo fi = new FileInfo(@"d:\t87.txt");
                StreamWriter sw = fi.CreateText();
                MessageBox.Show("Instantiate file before move" + fi.Name);

                //File is occupied by thread and cannot be moved
                fi.CopyTo(@"d:\Mobile 2.txt");
                
            }
           
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (File.Exists(@"d:\t87.txt"))
                    File.Delete(@"d:\t87.txt");
                else
                {
                    FileInfo fi = new FileInfo(@"d:\t87.txt");
                    StreamWriter sw = fi.CreateText();
                    MessageBox.Show("Instantiate file before deleting" + fi.Name);
                }

                MessageBox.Show("File deleted successfully!");
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
                return;
            }
        }
    }
}

Keywords: encoding Windows Mobile

Added by BinaryBird on Sun, 15 Dec 2019 19:30:03 +0200