C#创建和读取txt文件

创建一个txt文件

void Creattxt()
	{
		FileStream fs = new FileStream("demo.txt", FileMode.Create,FileAccess.Write);
        
         StreamWriter sr=new StreamWriter(fs);
		
		sr.WriteLine("Your txt message");
		
		sr.Close();
		fs.Close();
	}


读取txt文件内容

	void Readtxt()
	{
		FileStream fs = new FileStream("demo.txt", FileMode.Open);
         StreamReader sr=new StreamReader(fs);
		
		string str=sr.ReadLine();

		sr.Close();
		fs.Close();
	}


 

你可能感兴趣的:(C#创建和读取txt文件)