文件和文件夹操作——文件操作实列

1、获取文件大小

思路:使用File类的Open方法打开指定的文件,然后使用FileStream类的Length属性获取文件的长度。

(1)、File类的Open方法:用来打开指定路径上的FileStream,局域读/写访问权限语法格式:public static FileStream Open(string path,FileMode mode)

参数说明:path:要打开的文件。 mode :FileMode枚举值之一

枚举值

说明

CreateNew

指定操作系统应创建新文件

Create

指定操作系统应创建新文件,如果文件已经存在,它将被覆盖

Open

指定操作系统应打开现有的文件

OpenOrCreate

指定操作系统应打开文件(如果文件存在,否则创建新文件)

Truncate

指定操作系统打开现有文件。文件一旦打开,就将被截断文为零字节大小

Append

打开现有文件并查找到文件尾,或创建新文件。

(2)、FileStream类的Length属性

FileStream类的Length属性用来获取用字节表示的流长度,语法:

Public override long Length{get;}

注:程序中使用与文件相关的类时,都需要添加System.IO命名空间

private void button1_Click(object sender, EventArgs e)

        {

            OpenFileDialog openDialog = new OpenFileDialog();//创建打开文件对话框对象

            if (openDialog.ShowDialog()==DialogResult.OK)//判断是否选中文件

            {

   MessageBox.Show("文件长度:"+File.Open(openDialog.FileName,FileMode.Open).Length+"字节");//弹出消息对话框

            }

        }

2、获取文件的后缀名:

      OpenFileDialog openDialog = new OpenFileDialog();

            if (openDialog.ShowDialog()==DialogResult.OK)

            {

              string a = openDialog.FileName.Substring(openDialog.FileName.LastIndexOf(".") + 1, openDialog.FileName.Length - openDialog.FileName.LastIndexOf(".") - 1);

                MessageBox.Show(a);

            }

3、获取文件的创建时间:

  OpenFileDialog openDialog = new OpenFileDialog();

            if (openDialog.ShowDialog()==DialogResult.OK)

            {

                FileInfo f = new FileInfo(openDialog.FileName);

                MessageBox.Show(f.CreationTime.ToString());

            }

注:获取文件的最新修改时间:FileInfo.LastWriteTime.ToString();

4、获取文件名中禁止使用的字符

思路:本实例主要用到了Path类的 GetInvalidFileNameChars()方法,语法:

   public static char[] GetInvalidFileNameChars();

返回值:包含不允许在文件名中使用的字符数组。

注:Path类位于System.IO命名空间中。

例:

char[] aa = Path.GetInvalidFileNameChars();

            foreach (char a in aa)

            {

                listBox1.Items.Add(Convert.ToInt32(a).ToString());

            }

5、创建和删除文件

File类的Create方法指定路径中创建文件,该方法可重载方法,语法:

public static FileStream Create(string path);

参数说明:

Path:要创建的文件的路径及名称。

返回值:一个FileStream,它提供对path中指定的文件的读写访问。

File类的Delete方法用来删除指定的文件,如果不存在则不引发异常。语法:

   public static void Delete(string path);

例:

   private void btn_Create_Click(object sender, EventArgs e)

        {

            SaveFileDialog P_SaveFileDialog = new SaveFileDialog();//创建保存文件对话框对象

            if (P_SaveFileDialog.ShowDialog() == DialogResult.OK)//判断是否确定保存文件

            {

                File.Create(P_SaveFileDialog.FileName);//创建文件

            }

        }

 

        private void btn_Remove_Click(object sender, EventArgs e)

        {

            OpenFileDialog P_OpenFileDialog = new OpenFileDialog();//创建打开文件对话框对象

            if (P_OpenFileDialog.ShowDialog() == DialogResult.OK)//判断是否确定删除文件

            {

                File.Delete(P_OpenFileDialog.FileName);//删除文件

            }

        }

6、清空回收站

思路:清空回收站主要用到系统的API函数 SHEmptyRecycleBin,语法如下:

    [DllImportAttribute("shell32.dll")]                                       //声明API函数

private static extern int SHEmptyRecycleBin(IntPtr handle, string root, int falgs);

参数说明:handle:父窗口句柄。

Root:将要清空的回收站的地址,如果为null值时,将清空所有的驱动器上的回收站。

Falgs:用于清空回收站的功能参数。

注:使用系统API函数时,首先需要在命名空间区域添加System.Runtime.InterServices命名空间。

例:

   const int SHERB_NOCONFIRMATION = 0x000001;   //整型常量在API中表示删除时没有确认对话框

        const int SHERB_NOPROGRESSUI = 0x000002;                //在API中表示不显示删除进度条

        const int SHERB_NOSOUND = 0x000004;                          //在API中表示删除完毕时不播放声音

 

        [DllImportAttribute("shell32.dll")]                                       //声明API函数

        private static extern int SHEmptyRecycleBin(IntPtr handle, string root, int falgs);

 

        private void button1_Click(object sender, EventArgs e)

        {

            //清空回收站

            SHEmptyRecycleBin(this.Handle, "", SHERB_NOCONFIRMATION + SHERB_NOPROGRESSUI + SHERB_NOSOUND);

        }

7、修改文件属性

思路:修改文件属性用到了FileInfo类的Attributes属性,语法格式如下:

public FileAttributes Attributes { get; set; }

参数说明:

 

FileAttribute枚举之一。

例:

 

private void button2_Click(object sender, EventArgs e)

        {

            System.IO.FileInfo f = new System.IO.FileInfo(textBox1.Text);                          //实例化FileInfo类

            if (checkBox1.Checked == true)                                                                                  //如果只读复选框选中

            {

                f.Attributes = System.IO.FileAttributes.ReadOnly;                                    //设置文件为只读

            }

            if (checkBox2.Checked == true)                                                                       //如果系统复选框选中

            {

                f.Attributes = System.IO.FileAttributes.System;                                         //设置文件为系统

            }

            if (checkBox3.Checked == true)                                                                       //如果存档复选框选中

            {

                f.Attributes = System.IO.FileAttributes.Archive;                                        //设置文件为存档

            }

            if (checkBox4.Checked == true)                                                                       //如果隐藏复选框选中

            {

                f.Attributes = System.IO.FileAttributes.Hidden;                                         //设置文件为隐藏

            }

        }

8、长文件名转换成短文件名

GetShortPathName是一个内核API函数,该函数能将长文件名转换为短文件名,语法格式如下:

[DllImport("Kernel32.dll")]//声明API函数

private static extern Int16 GetShortPathName(string lpszLongPath, StringBuilder lpszShortPath, Int16 cchBuffer);

参数说明:

lpszLongPath:指定欲获取路径名的那个文件的名字。可以是个完整路径,或者由当前目录决定。

lpszShortPath:指定一个缓冲区,用于装载文件的短路径和文件名。

cchBuffer:lpszShortPath缓冲区长度

例:

    [DllImport("Kernel32.dll")]//声明API函数

        private static extern Int16 GetShortPathName(string lpszLongPath, StringBuilder lpszShortPath, Int16 cchBuffer);

 

        private void button1_Click(object sender, EventArgs e)

        {

            OpenFileDialog OFDialog = new OpenFileDialog();//创建OpenFileDialog对象

            if (OFDialog.ShowDialog() == DialogResult.OK)//判读是否选择了文件

            {

                textBox1.Text = OFDialog.FileName;//显示选择的文件名

                string longName = textBox1.Text;//记录选择的文件名

                StringBuilder shortName = new System.Text.StringBuilder(256);//创建StringBuilder对象

                GetShortPathName(longName, shortName, 256);//调用API函数转换成短文件名

                string myInfo = "长文件名:" + longName;//显示长文件名

                myInfo += "\n短文件名:" + shortName;//显示短文件名

                label2.Text = myInfo;

            }

        }

9、单一文件复制举例

例1、BackgroundWorker实现异步复制文件显示进度条百分比源代码

使用BackgroundWorker组件可以防止页面出现假死的情况。

界面:

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.IO;

namespace WangHao

{

    public partial class backgroundworder : Form

    {

        BackgroundWorker worker = null;

        public backgroundworder()

        {

            InitializeComponent();

            progressBar1.Minimum = 0;

            progressBar1.Maximum = 100;

            worker = new BackgroundWorker();

            worker.WorkerReportsProgress = true;

            worker.WorkerSupportsCancellation = true;

            worker.DoWork += new DoWorkEventHandler(worker_DoWork);

            worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);

            worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);

        }

        void worker_DoWork(object sender, DoWorkEventArgs e)

        {

          try

            {

                if (e.Argument != null && e.Argument is FilePair)

                {

                    FilePair obj = (FilePair)e.Argument;

                    string orgFile = obj.OrgFile;

                    string newFile = obj.NewFile;

                    FileStream readFileStream = new FileStream(orgFile, FileMode.Open, FileAccess.Read);

                    FileStream writeFileStream = new FileStream(newFile, FileMode.Create, FileAccess.Write);

                    DateTime start = DateTime.Now;

                    TimeSpan ts;

                    long totalByte = readFileStream.Length;

                    int buffLength = 1024 * 1024 * 64;

                    byte[] buff = new byte[buffLength];

                    long writtenByte = 0;

                    int everytimeReadByteLength = readFileStream.Read(buff, 0, buffLength);

                    while (everytimeReadByteLength > 0)

                    {

                        writeFileStream.Write(buff, 0, everytimeReadByteLength);

                        everytimeReadByteLength = readFileStream.Read(buff, 0, buffLength);

                        writtenByte += everytimeReadByteLength;

                        int percent = (int)(writtenByte * 100 / totalByte);

                        ts = DateTime.Now.Subtract(start);

                        double speed = (double)writtenByte / ts.TotalMilliseconds * 1000 / (1024 * 1024);

                        obj.Speed = speed;

                        worker.ReportProgress(percent, obj);//传递用户自定义对象

                    }

                    writeFileStream.Close();

                    readFileStream.Close();

                    worker.ReportProgress(100);

                }

            }

            catch (Exception ex)

            {

                worker.ReportProgress(100, ex);

            }

        }

        void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)

        {

            FilePair obj = e.UserState as FilePair;

            progressBar1.Value = e.ProgressPercentage;

            label3.Text = e.ProgressPercentage + "%";

            if (obj != null)

            {               

                lblMsg.Text = "正在复制,复制速度为" + obj.Speed.ToString() + "M";

            }

        }

        void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)

        {

            lblMsg.Text = "文件复制完成";

        }

        private void button1_Click(object sender, EventArgs e)

        {

            progressBar1.Value = 0;

            string orgFile = textBox1.Text.Trim();

            string newFile = textBox2.Text.Trim();

            if (System.IO.File.Exists(orgFile))

            {

                bool copy = true;

                if (System.IO.File.Exists(newFile))

                {

                    if (DialogResult.Cancel == MessageBox.Show("新文件已经存在,是不继续复制,新文件即被覆盖", "文件已存在", MessageBoxButtons.OKCancel))

                    {

                        copy = false;

                    }

                }

                if (copy)

                {

                    FilePair fileObj = new FilePair(orgFile, newFile);

                    lblMsg.Text = "文件开始复制";

                    worker.RunWorkerAsync(fileObj);

                }

            }

            else

            {

                lblMsg.Text = "源文件不存在";

            }

        }

    }

    public class FilePair

    {

        private string _orgFile;

        private string _newFile;

        public FilePair(string orgfile, string newfile)

        {

            _orgFile = orgfile;

            _newFile = newfile;

        }

        public string OrgFile

        {

            get { return _orgFile; }

            set { _orgFile = value; }

        }

        public string NewFile

        {

            get { return _newFile; }

            set { _newFile = value; }

        }

        public double Speed { get; set; }

    }

}

例2:多线程的方式复制单个文件

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;

using System.Threading;//线程序的命名空间

using Microsoft.Win32.SafeHandles;

using System.Runtime.InteropServices;

namespace FileCopyPlan

{

    public delegate void SpeedAndProgress(double speed, double progress);//定义委托

    public partial class Frm_Main : Form

    {

        public Frm_Main()

        {

            InitializeComponent();

        }

        private System.Threading.Thread thdAddFile; //创建一个线程

        private string str = "";

        private void button1_Click(object sender, EventArgs e)

        {

            if (openFileDialog1.ShowDialog() == DialogResult.OK)//打开文件对话框

                textBox1.Text = openFileDialog1.FileName;//获取源文件的路径

        }

        private void button2_Click(object sender, EventArgs e)

        {

            if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)//打开文件夹对话框

                textBox2.Text = folderBrowserDialog1.SelectedPath;//获取目的文件的路径

        }

        private void button3_Click(object sender, EventArgs e)

        {

            if (textBox1.Text.Length == 0 || textBox2.Text.Length == 0)

            {

                MessageBox.Show("请选择原文件路径或目的文件路径。");

                return;

            }

            str = textBox1.Text;//记录源文件的路径

            str = "\\" + str.Substring(str.LastIndexOf('\\') + 1, str.Length - str.LastIndexOf('\\') - 1);//获取源文件的名称

            thdAddFile = new Thread(new ThreadStart(RunAddFile));//创建一个线程

            thdAddFile.IsBackground = true;

            thdAddFile.Start();//执行当前线程

        }

        /// <summary>

        /// 对文件进行复制,并在复制完成后关闭线程

        /// </summary>

        public void RunAddFile()

        {

            FileOperation fo = new FileOperation();

            fo.OnSpeedAndProgress += new SpeedAndProgress(Frm_Main_OnSpeedAndProgress);

            fo.CopyFile(textBox1.Text, textBox2.Text + str, 1024 * 1024 * 64, progressBar1);//复制文件

            MessageBox.Show("复制完成", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

            thdAddFile.Abort();//关闭线程

        }

        /// <summary>

        /// 文件的复制

        /// </summary>

        /// <param FormerFile="string">源文件路径</param>

        /// <param toFile="string">目的文件路径</param>

        /// <param SectSize="int">传输大小</param>

        /// <param progressBar="ProgressBar">ProgressBar控件</param>

        void Frm_Main_OnSpeedAndProgress(double speed, double progress)

        {//防卡死的关键,设置进度的时候调用窗体显示,不调用的时候释放UI线程,这样就能保证主窗体不卡死

            this.BeginInvoke(new SpeedAndProgress(aa), new object[] { speed, progress });//在线程上执行指定的委托       

        }

        void aa(double speed, double progress)

        {

            progressBar1.Value = Convert.ToInt32(progress);

            label1.Text = speed.ToString();

            label1.Refresh();

        }

    }

    public class FileOperation

    {

        public event SpeedAndProgress OnSpeedAndProgress;

        public const short FILE_ATTRIBUTE_NORMAL = 0x80;

        public const short INVALID_HANDLE_VALUE = -1;

        public const uint GENERIC_READ = 0x80000000;

        public const uint GENERIC_WRITE = 0x40000000;

        public const uint CREATE_NEW = 1;

        public const uint CREATE_ALWAYS = 2;

        public const uint OPEN_EXISTING = 3;

        public const uint FILE_FLAG_NO_BUFFERING = 0x20000000;

        public const uint FILE_FLAG_WRITE_THROUGH = 0x80000000;

        public const uint FILE_SHARE_READ = 0x00000001;

        public const uint FILE_SHARE_WRITE = 0x00000002;

        // Use interop to call the CreateFile function.

        // For more information about CreateFile,

        // see the unmanaged MSDN reference library.

        [DllImport("kernel32.dll", SetLastError = true)]

        static extern SafeFileHandle CreateFile(string lpFileName, uint dwDesiredAccess,

        uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition,

        uint dwFlagsAndAttributes, IntPtr hTemplateFile);

        public void CopyFile(string FormerFile, string toFile, int SectSize, ProgressBar progressBar1)

        {

            bool useBuffer = true;

            SafeFileHandle fr = CreateFile(FormerFile, GENERIC_READ, FILE_SHARE_READ, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero);

            SafeFileHandle fw = CreateFile(toFile, GENERIC_WRITE, FILE_SHARE_READ, IntPtr.Zero, CREATE_ALWAYS, 0, IntPtr.Zero);

            progressBar1.Value = 0;//设置进度栏的当前位置为0

            progressBar1.Minimum = 0;//设置进度栏的最小值为0

            progressBar1.Maximum = 100;

            int bufferSize = useBuffer ? 1024 * 1024 * 64 : 1024 * 1024 * 64;

            FileStream fsr = new FileStream(fr, FileAccess.Read);

            FileStream fsw = new FileStream(fw, FileAccess.Write);

            BinaryReader br = new BinaryReader(fsr);

            BinaryWriter bw = new BinaryWriter(fsw);

            byte[] buffer = new byte[bufferSize];

            Int64 len = fsr.Length;

            DateTime start = DateTime.Now;

            TimeSpan ts;

            while (fsr.Position < fsr.Length)

            {

                int readCount = br.Read(buffer, 0, bufferSize);

                bw.Write(buffer, 0, readCount);

                ts = DateTime.Now.Subtract(start);

                double speed = (double)fsr.Position / ts.TotalMilliseconds * 1000 / (1024 * 1024);

                double progress = (double)fsr.Position / len * 100;

                if (OnSpeedAndProgress != null)

                {

                    OnSpeedAndProgress(speed, progress);

                }

            }

            br.Close();

            bw.Close();

        }

    }

}

10、C#操作INI文件

思路:对INI文件操作,主要用到了系统API函数GetPrivateProfileString和WritePrivateProfileString函数。

  1. GetPrivateProfileString该函数主要用来读取INI文件的内容。语法格式如下:[DllImport("kernel32")]

private static extern int GetPrivateProfileString(string lpAppName,string lpKeyName,

string lpDefault,StringBuilder lpReturnedString,int nSize,string lpFileName);

参数说明:

参数

说明

lpAppName

表示INI文件内部根节点的值

lpKeyName

表示根节点下子标记的值

lpDefault

表示当标记值未设定或不存在是的默认值。

lpReturnedString

表示返回读取节点的值

nSize

表示读取的节点内容的最大容量

lpFileName

表示文件的全路径

  1. WritePrivateProfileString函数用于向INI文件中写数据。

  [DllImport("kernel32")]

   private static extern long WritePrivateProfileString(string mpAppName,string mpKeyName,

string mpDefault,string mpFileName);

参数说明:

参数

说明

mpAppName

表示INI内部根节点的值

mpKeyName

表示将要修改的标记名称

mpDefault

表示想要修改的内容

mpFileName

表示INI文件的全路径

例:

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;

using System.Runtime.InteropServices;

namespace INIFileOperate

{

    public partial class Frm_Main : Form

    {

        public Frm_Main()

        {

            InitializeComponent();

        }

        #region 变量声明区

        public string str = "";//该变量保存INI文件所在的具体物理位置

        public string strOne = "";

        [DllImport("kernel32")]

        private static extern int GetPrivateProfileString(

            string lpAppName,

            string lpKeyName,

            string lpDefault,

            StringBuilder lpReturnedString,

            int nSize,

            string lpFileName);

        public string ContentReader(string area, string key, string def)

        {

            StringBuilder stringBuilder = new StringBuilder(1024);                                      //定义一个最大长度为1024的可变字符串

            GetPrivateProfileString(area, key, def, stringBuilder, 1024, str);                     //读取INI文件

            return stringBuilder.ToString();                                                                         //返回INI文件的内容

        }

        [DllImport("kernel32")]

        private static extern long WritePrivateProfileString(

            string mpAppName,

            string mpKeyName,

            string mpDefault,

            string mpFileName);

        #endregion

        #region 窗体加载部分

        private void Form1_Load(object sender, EventArgs e)

        {

            str = Application.StartupPath + "\\ConnectString.ini";                                                 //INI文件的物理地址

            strOne = System.IO.Path.GetFileNameWithoutExtension(str);                                //获取INI文件的文件名

            if (File.Exists(str))                                                                                                 //判断是否存在该INI文件

            {

                server.Text = ContentReader(strOne, "Data Source", "");                                 //读取INI文件中服务器节点的内容

                database.Text = ContentReader(strOne, "DataBase", "");                                //读取INI文件中数据库节点的内容

                uid.Text = ContentReader(strOne, "Uid", "");                                                        //读取INI文件中用户节点的内容

                pwd.Text = ContentReader(strOne, "Pwd", "");                                                    //读取INI文件中密码节点的内容

            }

        }

        #endregion

        #region 进行修改操作

        private void button1_Click(object sender, EventArgs e)

        {

            if (File.Exists(str))                                                                                                  //判断是否存在INI文件

            {

                WritePrivateProfileString(strOne, "Data Source", server.Text, str);               //修改INI文件中服务器节点的内容

                WritePrivateProfileString(strOne, "DataBase", database.Text, str);             //修改INI文件中数据库节点的内容

                WritePrivateProfileString(strOne, "Uid", uid.Text, str);                            //修改INI文件中用户节点的内容

                WritePrivateProfileString(strOne, "Pwd", pwd.Text, str);                        //修改INI文件中密码节点的内容

                MessageBox.Show("恭喜你,修改成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

            else

            {

                MessageBox.Show("对不起,你所要修改的文件不存在,请确认后再进行修改操作!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

        }

        #endregion

    }

}

11、创建PDF文件

思路:创建PDF文档用到了第三方组件itextsharp.dll
例:

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 iTextSharp.text;

using iTextSharp.text.pdf;

using System.IO;

namespace CreatePDFDocument

{

    public partial class Frm_Main : Form

    {

        public Frm_Main()

        {

            InitializeComponent();

        }

        //该变量保存PDF的文档名

        public static string filePath = "";

        //创建PDF文档

        private void button1_Click(object sender, EventArgs e)

        {

            SaveFileDialog saveFileDialog = new SaveFileDialog();                              //给出文件保存信息,确定保存位置

            saveFileDialog.Filter = "PDF文件(*.PDF)|*.PDF";

            if (saveFileDialog.ShowDialog() == DialogResult.OK)

            {

                filePath = saveFileDialog.FileName;

                //开始创建PDF文档,首先声明一个Document对象

                Document document = new Document();

                //使用指定的路径和创建模式初始化文件流对象

                PdfWriter.getInstance(document, new FileStream(filePath, FileMode.Create));

                document.Open();                                                                                       //打开文档

                BaseFont baseFont = BaseFont.createFont(@"c:\windows\fonts\SIMSUN.TTC,1", BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

                iTextSharp.text.Font font = new iTextSharp.text.Font(baseFont, 20); //设置文档字体样式

                document.Add(new Paragraph(richTextBox1.Text, font));                       //添加内容至PDF文档中

                document.Close();                                                                                       //关闭文档

                MessageBox.Show("祝贺你,文档创建成功!", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }

        }

    }

}

12、判断文件是否被占用

例:

using System.IO;

using System.Runtime.InteropServices;

[DllImport("kernel32.dll")]

public static extern IntPtr _lopen(string lpPathName, int iReadWrite);

[DllImport("kernel32.dll")]

public static extern bool CloseHandle(IntPtr hObject);

public const int OF_READWRITE = 2;

public const int OF_SHARE_DENY_NONE = 0x40;

public readonly IntPtr HFILE_ERROR = new IntPtr(-1);

private void button1_Click(object sender, EventArgs e)

{

    string vFileName = @"c:\temp\temp.bmp";

    if (!File.Exists(vFileName))

    {

        MessageBox.Show("文件都不存在!");

        return;

    }

    IntPtr vHandle = _lopen(vFileName, OF_READWRITE | OF_SHARE_DENY_NONE);

    if (vHandle == HFILE_ERROR)

    {

        MessageBox.Show("文件被占用!");

        return;

    }

    CloseHandle(vHandle);

    MessageBox.Show("没有被占用!");

}

13、实现文件的拖放操作

思路:实现文件的拖放操作主要用到了DragEventArgs类的Data属性及DataObject类的GetDataPresent方法。

  1. DragEventArgs类的Data属性

DragEventArgs类包含与所有拖放事件(DragEnter、DragLeave、DragOver和Drop)相关的参数,其Data属性用来读取一个数据对象,该对象包含与对应拖动事件关联的数据。Data属性的语法如下:

public IDataObject Data { get; }

参数说明:属性值,一个数据对象,该对象包含与对应拖动事件关联的数据。

  1. DataObject类的GetDataPresent方法

DataObject类主要实现数据传输机制,其GetDataPresent方法用来确定DataObject对象中存储的数据是否与指定的格式关联

bool GetDataPresent(string format);

参数说明:format:要检查的格式。返回值:如果DataObject对象存储的数据与指定的格式关联,或者可以转换指定的格式,则为True;否则为false。

  1. DataObject类的GetData方法

DataObject类的GetData方法用来返回与所指定数据格式关联的数据,语法如下:

object GetData(string format);

format:要检查的数据的格式。

返回值:与指定格式关联的数据,或未null。

例:

  public partial class Frm_Main : Form

    {

        public Frm_Main()

        {

            InitializeComponent();

        }

        private void Form1_Load(object sender, EventArgs e)

        {

            this.AllowDrop = true;

        }

        private void Form1_DragEnter(object sender, DragEventArgs e)

        {

            if (e.Data.GetDataPresent(DataFormats.FileDrop))

            {

                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);              //获取拖入文件的基本信息

                for (int i = 0; i < files.Length; i++)                                                                //拖放入窗体的文件的文件名加入ListBox

                {

                    listBox1.Items.Add(files[i]);                                                                                  //添加文件的路径

                }

            }

        }

}

14、将文件分割为多个小文件和合并

思路:对文件进行分割操作主要通过FileStream、BinaryReader和BinaryWriter类实现的。以文件的全路径对应的字符串和文件打开模式来初始化FileStream文件流,然后再以FileStream文件流来初始化BinaryReader文件阅读器,最后通过for语句循环将大文件分割成为多个小文件。

  1. BinaryReader类,该类主要用特定的编码将基元数据类型读作二进制值,其构造函数主要使用UTF8Encoding初始化BinaryReader类的实例。语法如下:

public BinaryReader(Stream input);

分割文件用到了BinaryReader类的ReadBytes方法,该方法用来从当前流中将count个字节读入字节数组,并是当前位置提升count个字节。语法如下:

public virtual byte[] ReadBytes(int count);

  1. BinaryWriter类,以二进制形式将济源类型写入流,并支持用特定的编码写入字符串,其构造函数主要使用UTF-8作为字符串编码来初始化BinaryWriter类的实例,语法如下:public BinaryWriter(Stream output);

将内容写入分割后的文件中用到了BinaryWriter类的Write方法,该方法用来将值写入当前,语法如下:public virtual void Write(byte[] buffer);

例:

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 FileComminuteUnite

{

    public partial class Frm_Main : Form

    {

        public Frm_Main()

        {

            InitializeComponent();

        }

        #region 分割文件

        /// <summary>

        /// 分割文件

        /// </summary>

        /// <param name="strFlag">分割单位</param>

        /// <param name="intFlag">分割大小</param>

        /// <param name="strPath">分割后的文件存放路径</param>

        /// <param name="strFile">要分割的文件</param>

        /// <param name="PBar">进度条显示</param>

        public void SplitFile(string strFlag, int intFlag, string strPath, string strFile, ProgressBar PBar)

        {

            int iFileSize = 0;

            //根据选择来设定分割的小文件的大小

            switch (strFlag)

            {

                case "Byte":

                    iFileSize = intFlag;

                    break;

                case "KB":

                    iFileSize = intFlag * 1024;

                    break;

                case "MB":

                    iFileSize = intFlag * 1024 * 1024;

                    break;

                case "GB":

                    iFileSize = intFlag * 1024 * 1024 * 1024;

                    break;

            }

            FileStream SplitFileStream = new FileStream(strFile, FileMode.Open);//以文件的全路径对应的字符串和文件打开模式来初始化FileStream文件流实例

            BinaryReader SplitFileReader = new BinaryReader(SplitFileStream);//以FileStream文件流来初始化BinaryReader文件阅读器

            byte[] TempBytes;//每次分割读取的最大数据

            int iFileCount = (int)(SplitFileStream.Length / iFileSize);//小文件总数

            PBar.Maximum = iFileCount;

            if (SplitFileStream.Length % iFileSize != 0) iFileCount++;

            string[] TempExtra = strFile.Split('.');

            //循环将大文件分割成多个小文件

            for (int i = 1; i <= iFileCount; i++)

            {

                //确定小文件的文件名称

                string sTempFileName = strPath + @"\" + i.ToString().PadLeft(4, '0') + "." + TempExtra[TempExtra.Length - 1]; //小文件名

                FileStream TempStream = new FileStream(sTempFileName, FileMode.OpenOrCreate);  //根据文件名称和文件打开模式来初始化FileStream文件流实例

                BinaryWriter TempWriter = new BinaryWriter(TempStream);   //以FileStream实例来创建、初始化BinaryWriter书写器实例

                TempBytes = SplitFileReader.ReadBytes(iFileSize); //从大文件中读取指定大小数据

                TempWriter.Write(TempBytes);    //把此数据写入小文件

                TempWriter.Close();   //关闭书写器,形成小文件

                TempStream.Close();     //关闭文件流

                PBar.Value = i - 1;

            }

            SplitFileReader.Close();//关闭大文件阅读器

            SplitFileStream.Close();

            MessageBox.Show("文件分割成功!");

        }

        #endregion

        private void frmSplit_Load(object sender, EventArgs e)

        {

            timer1.Start();//启动计时器

        }

        //选择要分割的文件

        private void btnSFile_Click(object sender, EventArgs e)

        {

            if (openFileDialog.ShowDialog() == DialogResult.OK)

            {

                txtFile.Text = openFileDialog.FileName;

            }

        }

        //执行文件分割操作

        private void btnSplit_Click(object sender, EventArgs e)

        {

            try

            {

                if (txtLength.Text == ""||txtFile.Text.Trim()==""||txtPath.Text.Trim()=="")

                {

                    MessageBox.Show("请将信息填写完整!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    txtLength.Focus();

                }

                else if (cboxUnit.Text == "")

                {

                    MessageBox.Show("请选择要分割的文件单位!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    cboxUnit.Focus();

                }

                else

                {

                    SplitFile(cboxUnit.Text, Convert.ToInt32(txtLength.Text.Trim()), txtPath.Text, txtFile.Text, progressBar);

                }

            }

            catch { }

        }

        //选择分割后的文件存放路径

        private void btnSPath_Click(object sender, EventArgs e)

        {

            if (folderBrowserDialog.ShowDialog() == DialogResult.OK)

            {

                txtPath.Text = folderBrowserDialog.SelectedPath;

            }

        }

        //监视“分割”/“合并”按钮的可用状态

        private void timer1_Tick(object sender, EventArgs e)

        {

            if (txtFile.Text != "" && txtPath.Text != "")

                btnSplit.Enabled = true;

            else

                btnSplit.Enabled = false;

        }

    }

}

对文件合并

 

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 FileComminuteUnite

{

    public partial class Frm_Main : Form

    {

        public Frm_Main()

        {

            InitializeComponent();

        }

        #region 合并文件

        /// <summary>

        /// 合并文件

        /// </summary>

        /// <param name="list">要合并的文件集合</param>

        /// <param name="strPath">合并后的文件名称</param>

        /// <param name="PBar">进度条显示</param>

        public void CombinFile(string[] strFile, string strPath, ProgressBar PBar)

        {

            PBar.Maximum = strFile.Length;

            FileStream AddStream = null;

            //以合并后的文件名称和打开方式来创建、初始化FileStream文件流

            AddStream = new FileStream(strPath, FileMode.Append);

            //以FileStream文件流来初始化BinaryWriter书写器,此用以合并分割的文件

            BinaryWriter AddWriter = new BinaryWriter(AddStream);

            FileStream TempStream = null;

            BinaryReader TempReader = null;

            //循环合并小文件,并生成合并文件

            for (int i = 0; i < strFile.Length; i++)

            {

                //以小文件所对应的文件名称和打开模式来初始化FileStream文件流,起读取分割作用

                TempStream = new FileStream(strFile[i].ToString(), FileMode.Open);

                TempReader = new BinaryReader(TempStream);

                //读取分割文件中的数据,并生成合并后文件

                AddWriter.Write(TempReader.ReadBytes((int)TempStream.Length));

                //关闭BinaryReader文件阅读器

                TempReader.Close();

                //关闭FileStream文件流

                TempStream.Close();

                PBar.Value = i + 1;

            }

            //关闭BinaryWriter文件书写器

            AddWriter.Close();

            //关闭FileStream文件流

            AddStream.Close();

            MessageBox.Show("文件合并成功!");

        }

        #endregion

        private void frmSplit_Load(object sender, EventArgs e)

        {

            timer1.Start();//启动计时器

        }

        //选择要合成的文件

        private void btnCFile_Click(object sender, EventArgs e)

        {

            if (openFileDialog.ShowDialog() == DialogResult.OK)

            {

                string Selectfile = "";

                string[] files = openFileDialog.FileNames;

                for (int i = 0; i < files.Length; i++)

                {

                    Selectfile += "," + files[i].ToString();

                }

                if (Selectfile.StartsWith(","))

                {

                    Selectfile = Selectfile.Substring(1);

                }

                if (Selectfile.EndsWith(","))

                {

                    Selectfile.Remove(Selectfile.LastIndexOf(","),1);

                }

                txtCFile.Text = Selectfile;

            }

        }

        //选择合成后的文件存放路径

        private void btnCPath_Click(object sender, EventArgs e)

        {

            if (saveFileDialog.ShowDialog() == DialogResult.OK)

            {

                txtCPath.Text = saveFileDialog.FileName;

            }

        }

        //执行合成文件操作

        private void btnCombin_Click(object sender, EventArgs e)

        {

            try

            {

                if (txtCFile.Text.Trim() == "" || txtCPath.Text.Trim() == "")

                {

                    MessageBox.Show("请将信息输入完整!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                }

                else

                {

                    if (txtCFile.Text.IndexOf(",") == -1)

                        MessageBox.Show("请选择要合成的文件,最少为两个!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

                    else

                    {

                        string[] strFiles = txtCFile.Text.Split(',');

                        CombinFile(strFiles, txtCPath.Text, progressBar);

                    }

                }

            }

            catch { }

        }

        //监视“合并”按钮的可用状态

        private void timer1_Tick(object sender, EventArgs e)

        {

            if (txtCFile.Text != "" && txtCPath.Text != "")

                btnCombin.Enabled = true;

            else

                btnCombin.Enabled = false;

        }

    }

}