C#对文件的操作

一、实验目的:

1. 熟悉.NET框架下路径及文件信息的获取方法。

2. 熟悉文本文件、二进制流式文件的读取和保存方法。

二、实验内容及要求:

1、实现选择一个路径,并显示该路径下的所有目录及文件。

2、编程实现显示选中文件的属性信息(大小、最后访问时间、最后修改时间等信息)

 

3、创建应用程序,实现在窗体关闭后,下次打开时能自动保存窗体的大小和位置(用文本文件保存)。

4、继续完善数据库操作类,实现:将数据库连接信息(服务器IP地址、数据库名)用文本文件配置,在数据库操作类中提供方法从配置文件中读入服务器IP地址、数据库名,实现连接字符串的动态生成。

 

5、编程实现,将教师信息:教师号(Int16),教师名(string),性别(bool),基本工资(double),写入二进制文件保存。同时实现从二进制文件中读出写入内容。

 

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;

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

        private void button1_Click(object sender, EventArgs e)
        {
            listBox1.Items.Clear();
            string curDir = "";
            string subDir = "";
            string curFile = "";
            try
            {
                FolderBrowserDialog fbd = new FolderBrowserDialog();
                if (fbd.ShowDialog() == DialogResult.OK)
                {
                    curDir = fbd.SelectedPath;
                    string[] surDirs = Directory.GetDirectories(curDir);
                    string[] Files = Directory.GetFiles(curDir);
                    for (int i = 0; i < subDir.Length; i++)
                    {
                        subDir = surDirs[i];
                        subDir = subDir.Substring(subDir.LastIndexOf("\\") + 1);
                        listBox1.Items.Add(subDir);
                    }
                    for (int k = 0; k < Files.Length; k++)
                    {
                        curFile = Files[k];
                        curFile = curFile.Substring(curFile.LastIndexOf("\\") + 1);
                        listBox2.Items.Add(curFile);
                    }
                }
            }
            finally
            {      }


        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                FileInfo file = new FileInfo(ofd.FileName);

                MessageBox.Show("文件名: " + file.Name + "\n" +
                    "大小: " + file.Length.ToString() + "\n" +
                    "最后的访问时间: " + file.LastAccessTime.ToString() + "\n"
                    + "最后的修改时间: " + file.LastWriteTime.ToString() + "\n" + "所在目录: " + file.DirectoryName);

            }
        }
    }

}
后台对文件的操作

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;

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

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            int x, y, w, h;
            x = this.Left;
            y = this.Top;
            w = this.Width;
            h = this.Height;
            StreamWriter sw = File.CreateText(Application.StartupPath + "\\config.txt");
            sw.WriteLine("x=" + x.ToString());
            sw.WriteLine("y=" + y.ToString());
            sw.WriteLine("w=" + w.ToString());
            sw.WriteLine("h=" + h.ToString());
            sw.Flush();
            sw.Close();
            sw.Dispose();


        }

        private void Form1_Load(object sender, EventArgs e)
        {
            int x, y, w, h;
            string ss = "";
            StreamReader sr = File.OpenText(Application.StartupPath + "\\config.txt");
            ss = sr.ReadLine();
            x = Convert.ToInt16(ss.Substring(2));
            ss = sr.ReadLine();
            y = Convert.ToInt16(ss.Substring(2));
            ss = sr.ReadLine();
            w = Convert.ToInt16(ss.Substring(2));
            ss = sr.ReadLine();
            h = Convert.ToInt16(ss.Substring(2));
            ss = sr.ReadLine();
            this.Left = x;
            this.Top = y;
            this.Width = w;
            this.Height = h;
            sr.Close();
            sr.Dispose();



        }
    }
}

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;

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

        struct teacher
        {
            public Int16 jsh;
            public string jsm;
            public bool xb;
            public double gz;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            teacher t;
            t.jsh = Convert.ToInt16(textBox1.Text);
            t.jsm = textBox2.Text;
            if (radioButton1.Checked)
                t.xb = true;
            else
                t.xb = false;
            t.gz = Convert.ToDouble(textBox3.Text);
            SaveFileDialog sf = new SaveFileDialog();
            if (sf.ShowDialog() == DialogResult.OK)
            {
                FileStream fs = new FileStream(sf.FileName, FileMode.Create);
                BinaryWriter bw = new BinaryWriter(fs);
                bw.Write((Int16)t.jsh);
                bw.Write((string)t.jsm);
                bw.Write((bool)t.xb);
                bw.Write((double)t.gz);
                bw.Flush();
                fs.Close();
            }


        }

        private void button2_Click(object sender, EventArgs e)
        {
            teacher t;
            OpenFileDialog od = new OpenFileDialog();
            if (od.ShowDialog() == DialogResult.OK)
            {
                FileStream fs = new FileStream(od.FileName, FileMode.Open);
                BinaryReader br = new BinaryReader(fs);
                t.jsh = br.ReadInt16();
                t.jsm = br.ReadString();
                t.xb = br.ReadBoolean();
                t.gz = br.ReadDouble();
                textBox1.Text = t.jsh.ToString();
                textBox2.Text = t.jsm;
                if (t.xb)

                    radioButton1.Checked = true;

                else
                    radioButton2.Checked = true;


                textBox3.Text = t.gz.ToString();
                fs.Close();
            }
        }
    }
}
View Code

 

posted on 2015-03-19 09:18  波澜不惊super  阅读(323)  评论(0编辑  收藏  举报

导航