C# 操作文件

C#追加文件   

StreamWriter sw = File.AppendText(Server.MapPath(".")+"\myText.txt");   
sw.WriteLine("追逐理想");   
sw.WriteLine("kzlll");   
sw.WriteLine(".NET笔记");   
sw.Flush();   
sw.Close();   
 
C#拷贝文件   

string OrignFile,NewFile;   
OrignFile = Server.MapPath(".")+"\myText.txt";   
NewFile = Server.MapPath(".")+"\myTextCopy.txt";   
File.Copy(OrignFile,NewFile,true);   
 
C#删除文件   

string delFile = Server.MapPath(".")+"\myTextCopy.txt";   
File.Delete(delFile);   
 
C#移动文件   

string OrignFile,NewFile;   
OrignFile = Server.MapPath(".")+"\myText.txt";   
NewFile = Server.MapPath(".")+"\myTextCopy.txt";   
File.Move(OrignFile,NewFile);   
 
C#创建目录   

// 创建目录c:sixAge   
DirectoryInfo d=Directory.CreateDirectory("c:\sixAge");   
// d1指向c:sixAgesixAge1   
DirectoryInfo d1=d.CreateSubdirectory("sixAge1");   
// d2指向c:sixAgesixAge1sixAge1_1   
DirectoryInfo d2=d1.CreateSubdirectory("sixAge1_1");   
// 将当前目录设为c:sixAge   
Directory.SetCurrentDirectory("c:\sixAge");   
// 创建目录c:sixAgesixAge2   
Directory.CreateDirectory("sixAge2");   
// 创建目录c:sixAgesixAge2sixAge2_1   
Directory.CreateDirectory("sixAge2\sixAge2_1");  

 
下面是练习测试时加的

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
//using System.IO;

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


        private void add_Click(object sender, EventArgs e)
        {
            this.dataGridView1.Rows.Add(this.textBox1.Text.Trim(), this.textBox2.Text.Trim(), this.textBox3.Text.Trim(), this.textBox4.Text.Trim());
           
            this.dataGridView1.Refresh();
           
        }

        private void display_Click(object sender, EventArgs e)
        {
            //取第几行第几列的值
            MessageBox.Show(this.dataGridView1.Rows[1].Cells[0].Value.ToString());
           
        }
        public static Class1 cc = new Class1();

        private const string FILE_NAME = "c:\\test.txt";

        private void button1_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(this.dataGridView1.Rows[0].Cells[0].Value.ToString());
            // Compose a string that consists of three lines.
            string[] arra ={ "", "", "", "" };
            //if (System.IO.File.Exists(FILE_NAME))    //txnUdDateTime
            //{
            try
            {
                DataView dt = new DataView();

                string mysql = "select DeviceCode,OperatorCode,StationCode,txnUdDateTime ,count(*),sum(TransactionValue) FROM TUDMain WHERE udType<>0 and udSubType<>0 GROUP BY OperatorCode,TransactionCode,DeviceCode,OperatorCode,StationCode,txnUdDateTime";
                //string mysql = "select OperatorCode,TransactionCode,count(*),sum(TransactionValue)  FROM TUDMain WHERE udType<>0 and udSubType<>0 GROUP BY OperatorCode,TransactionCode";
                dt = (DataView)(cc.GetData(mysql));
                //this.dataGridView1.DataSource = dt.Table;
                //MessageBox.Show(dt.Table.Rows[0][0].ToString());

                System.IO.StreamWriter file = new System.IO.StreamWriter(FILE_NAME,true);
                //file.WriteLine(dt.Table);
                int rowsa = dt.Table.Rows.Count;
                int columnsa = dt.Table.Columns.Count;

                for (int i = 0; i < rowsa; i++)
                {
                    string mytxt = ""; ;
                    for (int j = 0; j < columnsa; j++)
                    {
                        mytxt += dt.Table.Rows[i][j].ToString() + "|";
                    }
                    mytxt += "\r\n";
                    file.WriteLine(mytxt);

                    //arra[0] = this.dataGridView1.Rows[i].Cells[0].Value.ToString();
                    //arra[1] = this.dataGridView1.Rows[i].Cells[1].Value.ToString();
                    //arra[2] = this.dataGridView1.Rows[i].Cells[2].Value.ToString();
                    //arra[3] = this.dataGridView1.Rows[i].Cells[3].Value.ToString();

                    //string a = "";
                    //string b = "";
                    //string c = "";
                    //string d = "";
                    //a = this.dataGridView1.Rows[i].Cells[0].Value.ToString();
                    //b = this.dataGridView1.Rows[i].Cells[1].Value.ToString();
                    //c = this.dataGridView1.Rows[i].Cells[2].Value.ToString();
                    //d = this.dataGridView1.Rows[i].Cells[3].Value.ToString();
                    //string lines = "First line.\r\nSecond line.\r\nThird line.";
                    //string lines = "编号:" + arra[0] + " 姓名:" + arra[1] + "  数学:" + arra[2] + "  语文:" + arra[3] + "\r\n";
                    //string lines = "编号:" + a + " 姓名:" + b + "  数学:" + c + "  语文:" + d + "\r\n";
                    // Write the string to a file.
                    //file.WriteLine(lines);
                }
                file.WriteLine("\r\n");
                file.Close();
                MessageBox.Show("file OK !");
            }
            catch (Exception err) { MessageBox.Show(err.ToString()); }
            //}
            //else
            //{
            //    MessageBox.Show("file is not found !");
            //}
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (System.IO.File.Exists(FILE_NAME))
            {
                using (System.IO.StreamWriter sw = new System.IO.StreamWriter(FILE_NAME))
                {
                    // Add some text to the file.
                    sw.Write("This is the ");
                    sw.WriteLine("header for the file.");
                    sw.WriteLine("-------------------");
                    // Arbitrary objects can also be written to the file.
                    sw.Write("The date is: ");
                    sw.WriteLine(DateTime.Now);
                    MessageBox.Show("ok");
                }
            }
            else
            {
                MessageBox.Show("file is not found !");
            }

        }
    }
}

原文地址:https://www.cnblogs.com/winnxm/p/911067.html