文件流操作

1. 引用

using System.IO;

2.写入相应文件夹

 //【1】创建文件流
FileStream fs=new FileStream(Application.StartupPath+"\\myfile.txt",FileMode.Create);
//【2】创建写入器
StreamWriter sw=new StreamWriter(fs);
//【3】以流的方式写入数据
sw.Write("  填写信息     ")
//【4】关闭写入器
sw.Close();
//【5】关闭文件流
fs.Close()

3.读取文件

 //【1】创建文件流
FileStream fs=new FileStream(Application.StartupPath+"\\myfile.txt",FileMode.Open);
 //【2】创建读取器
StreamReader sr=new StreamReader(fs);
//【3】以流的方式读取数据
string ReadText=sr.ReadToEnd();
//【4】关闭读取器
sr.Close();
//【5】关闭文件流
fs.Close()

4.模拟写入系统日志

//【1】创建文件流(文件模式为:追加)
FileStream fs=new FileStream(Application.StrartupPath+"\\myfile.txt",FileMode.Append);
 //【2】创建写入器
 StreamWriter sw=new StreamWriter(fs);
 //【3】以流的方式“逐行”写入数据
 sw.WriteLine(DateTime.Now.ToString()+"文件操作正常!")//【4】关闭读取器
sr.Close();
//【5】关闭文件流
fs.Close()

5.删除文件

File.Delete(this.path);

6.复制文件

if(File.Exists(this.Topath))
{
		File.Delete(this.Topath);
}
//this.Frompath 来源文件地址  
//this.Topath 目标文件地址
File.Copy(this.Frompath,this.Topath);

7.移动文件

if(File.Exists(this.Topath))
{
		File.Delete(this.Topath);
}
if(File.Exists(this.Frompath))
{
	File.Move(this.Frompath,this.Topath);
}

8.获取当前目录下的文件

string[] files=Directory.GetFiles("C:\\Myfiles");
foreach(string item in files)
{
	this.content+=item+"\r\n";
}

9.获取指定目录下的所有子目录

string[] dirs=Directory.GetDirectories("C:\\Myfiles");
foreach(string item in dirs)
{
	this.content+=item+"\r\n";
}

10.删除指定目录下的所有子目录和文件

 //使用DirectoryInfo对象,可以删除不为空的目录
DirectoryInfo dir = new DirectoryInfo("C:\\Myfiles");
dir.Delete(true);

11.封装对象信息,保存在文件中

class Student
{
    public string Name { get; set; }
    public string Gender { get; set; }
    public int Age { get; set; }
    public DateTime Birthday { get; set; }
}
 Student objStu = new Student()
            {
                Name = this.txtName.Text.Trim(),
                Age = Convert.ToInt16(this.txtAge.Text.Trim()),
                Gender = this.txtGender.Text.Trim(),
                Birthday = Convert.ToDateTime(this.txtBirthday.Text.Trim())
            };
              FileStream fs = new FileStream("C:\\objStu.obj", FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            //一行一行写入文本
            sw.WriteLine(objStu.Name);
            sw.WriteLine(objStu.Age);
            sw.WriteLine(objStu.Gender);
            sw.WriteLine(objStu.Birthday.ToShortDateString());
            //关闭文件流和写入器
            sw.Close();
            fs.Close();

12.读取封装信息

 FileStream fs = new FileStream("C:\\objStu.obj", FileMode.Open);
 StreamReader sr=new StreamReader(fs);
 Student objStu=new Student()
 {
               Name = sr.ReadLine(),
                Age = Convert.ToInt16(sr.ReadLine()),
                Gender = sr.ReadLine(),
                Birthday = Convert.ToDateTime(sr.ReadLine())
}
sr.Close();
fs.Close();
this.txtName.Text = objStu.Name;
this.txtAge.Text = objStu.Age.ToString();
this.txtGender.Text = objStu.Gender;
this.txtBirthday.Text = objStu.Birthday.ToShortDateString();

13.写入.ini 文件

引用文件
public class IniConfigHelper
    {
        #region API函数声明

        [DllImport("kernel32")]//返回0表示失败,非0为成功
        private static extern long WritePrivateProfileString(string section, string key,
            string val, string filePath);

        [DllImport("kernel32")]//返回取得字符串缓冲区的长度
        private static extern long GetPrivateProfileString(string section, string key,
            string def, StringBuilder retVal, int size, string filePath);


        #endregion

        #region 读Ini文件

        public static string ReadIniData(string Section, string Key, string NoText, string iniFilePath)
        {
            if (File.Exists(iniFilePath))
            {
                StringBuilder temp = new StringBuilder(1024);
                GetPrivateProfileString(Section, Key, NoText, temp, 1024, iniFilePath);
                return temp.ToString();
            }
            else
            {
                return String.Empty;
            }
        }

        #endregion

        #region 写Ini文件

        public static bool WriteIniData(string Section, string Key, string Value, string iniFilePath)
        {
            if (File.Exists(iniFilePath))
            {
                long OpStation = WritePrivateProfileString(Section, Key, Value, iniFilePath);
                if (OpStation == 0)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            else
            {
                return false;
            }
        }
 if (!File.Exists(ConfigPath))
            {
                FileStream fs = new FileStream(ConfigPath, FileMode.Create);
                fs.Close();
            }

            bool Result = true;

            Result&= IniConfigHelper.WriteIniData("PLC参数", "IP", this.txt_IP.Text.Trim(), ConfigPath);

            Result &= IniConfigHelper.WriteIniData("PLC参数", "Port", this.txt_Port.Text.Trim(), ConfigPath);

            if (Result)
            {
                MessageBox.Show("保存成功", "保存参数");
            }
            else
            {
                MessageBox.Show("保存失败", "保存参数");
            }

14.读取.ini文件

 this.txt_IP.Text = IniConfigHelper.ReadIniData("PLC参数", "IP", "", ConfigPath);
 this.txt_Port.Text = IniConfigHelper.ReadIniData("PLC参数", "Port", "", ConfigPath);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值