关于”文件合并“代码的问题
大一新生一枚,照着课本做一个文件分割与文件合并工具软件,文件分割没什么问题,但文件合并总是出问题。举个例子,分割出来三个文件,照课本上说的只需要选择分割文件中编号“0001”的小文件,通过Directory类的GetFiles找到源文件所在目录中文件名只编号不同的所有文件,计算出该类型文件的树木,通过循环按此目录文件名称的顺序读取文件,并使用BinaryWriter不断追加,循环技术及合并文件完成。但是我做出来后选择编号“0001”文件后合并虽然名字变了但内容还是“0001”的内容,照着课本打的代码,不知道哪错了??求助!!!!
文件合并的代码如下:
private void btnMerge_Click(object sender, EventArgs e)
{
string filename = "";
string[] arrFileNames;
int iFileCount = 0;
FileStream desMergeStream = null;
BinaryWriter desMergeBinaryWriter = null;
FileStream srcStream = null;
BinaryReader srcReader = null;
try
{
filename = fileName + "*." + fileExtra;
arrFileNames = Directory.GetFiles(srcMerFilename, filename);
iFileCount = arrFileNames.Length;
filename = fileName.Substring(0, fileName.Length - 4);
progBarMerge.Maximum = iFileCount;
desMergeStream = new FileStream(txtMergDes.Text + "\\" + filename + "." + fileExtra, FileMode.OpenOrCreate);
desMergeBinaryWriter = new BinaryWriter(desMergeStream);
for (int i = 0; i < iFileCount; i++)
{
srcStream = new FileStream(arrFileNames[i], FileMode.Open);
srcReader = new BinaryReader(srcStream);
desMergeBinaryWriter.Write(srcReader.ReadBytes((int)srcStream.Length));
srcReader.Close();
srcStream.Close();
progBarMerge.Value = i + 1;
}
desMergeBinaryWriter.Close();
desMergeStream.Close();
MessageBox.Show("成功合并!");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
if (desMergeStream != null) desMergeStream.Close();
if (desMergeBinaryWriter != null) desMergeBinaryWriter.Close();
if (srcStream != null) srcStream.Close();
if (srcReader != null) srcReader.Close();
txtMerSrc.Clear();
txtMergDes.Clear();
srcMerFilename = null;
desMerFilename = null;
fileName = "";
fileExtra = "";
srcMerFilename = "";
progBarMerge.Value = 0;
}
}
整个软件的代码如下:
using System;
using System.Collections.Generic;
using
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using
namespace WindowsFormsApplication1
{
public partial class MainFrm : Form
{
string srcSplFilename = null;
string desSplFilename = null;
string srcMerFilename = null;
string desMerFilename = null;
string fileName = "";
string fileExtra = "";
public MainFrm()
{
InitializeComponent();
}
private void MainFrm_Load(object sender, EventArgs e)
{
}
private void btnSource_Click(object sender, EventArgs e)
{
OpenFileDialog openDlg = new OpenFileDialog();
if (openDlg.ShowDialog() == DialogResult.OK && openDlg.FileName != string.Empty)
{
srcSplFilename = openDlg.FileName;
txtSourceDir.Text = srcSplFilename;
}
}
private void btnDes_Click(object sender, EventArgs e)
{
FolderBrowserDialog folderDlg = new FolderBrowserDialog();
folderDlg.Description = "请选择目的文件存放的文件夹";
if (folderDlg.ShowDialog() == DialogResult.OK)
{
desSplFilename = folderDlg.SelectedPath;
txtDesDir.Text = desSplFilename;
}
else MessageBox.Show("您没有选择目录");
}
private void btnOK_Click(object sender, EventArgs e)
{
int desFileSize = 0;
int desFileCount = 0;
byte[] buffer;
string[] desExtra;
string filename;
FileStream splitFileStream = null;
BinaryReader splitFileReader = null;
FileStream desFileStream = null;
BinaryWriter desBinaryWriter = null;
try
{
splitFileStream = new FileStream(srcSplFilename, FileMode.Open);
splitFileReader = new BinaryReader(splitFileStream);
desFileSize = int.Parse(cboSize.SelectedItem.ToString()) * 1024 * 1024;
desFileCount = (int)(splitFileStream.Length / desFileSize);
if (splitFileStream.Length % desFileSize != 0)
desFileCount++;
desExtra = srcSplFilename.Split('.');
filename = srcSplFilename.Substring(srcSplFilename.LastIndexOf("\\") + 1,
srcSplFilename.LastIndexOf(".") - (srcSplFilename.LastIndexOf("\\") + 1));
progBarSplit.Maximum = desFileCount - 1;
for (int i = 1; i <= desFileCount; i++)
{
string desFileName = @desSplFilename + @"\" + filename + i.ToString().PadLeft(4, '0') + "." + desExtra[desExtra.Length - 1];
desFileStream = new FileStream(desFileName, FileMode.OpenOrCreate);
desBinaryWriter = new BinaryWriter(desFileStream);
buffer = splitFileReader.ReadBytes(desFileSize);
desBinaryWriter.Write(buffer);
desBinaryWriter.Close();
desFileStream.Close();
progBarSplit.Value = i - 1;
}
splitFileReader.Close();
splitFileStream.Close();
MessageBox.Show("分割成功!");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
if (desBinaryWriter != null) desBinaryWriter.Close();
if (desFileStream != null) desFileStream.Close();
if (splitFileReader != null) splitFileReader.Close();
if (splitFileStream != null) splitFileStream.Close();
txtSourceDir.Clear();
txtDesDir.Clear();
txtSourceDir.Focus();
desSplFilename = null;
srcSplFilename = null;
cboSize.SelectedIndex = 0;
progBarSplit.Value = 0;
}
}
private void btnMergSrc_Click(object sender, EventArgs e)
{
string[] srcPath;
string[] filename;
string strTemp = "";
int i = 0;
OpenFileDialog openDlg = new OpenFileDialog();
openDlg.Title = "请选择要合并的第一个文件";
if (openDlg.ShowDialog() == DialogResult.OK && openDlg.FileName != "")
{
txtMerSrc.Text = openDlg.FileName;
}
srcPath = openDlg.FileName.Split(@"\".ToCharArray());
for (i = 0; i < srcPath.Length - 1; i++)
{
strTemp = strTemp + srcPath[i] + @"\";
}
srcMerFilename = strTemp;
filename = srcPath[srcPath.Length - 1].Split(@".".ToCharArray());
for (i = 0; i < filename.Length - 1; i++)
{
fileName = fileName + filename[i] + @".";
}
fileName = fileName.Substring(0, fileName.Length - 1);
fileExtra = filename[filename.Length - 1];
}
private void btnMergDes_Click(object sender, EventArgs e)
{
FolderBrowserDialog folderDlg = new FolderBrowserDialog();
folderDlg.Description = "请选择目的文件存放的文件夹";
if (folderDlg.ShowDialog() == DialogResult.OK)
{
desMerFilename = folderDlg.SelectedPath;
txtMergDes.Text = desMerFilename;
}
else MessageBox.Show("你没有选择目录");
}
private void btnMerge_Click(object sender, EventArgs e)
{
string filename = "";
string[] arrFileNames;
int iFileCount = 0;
FileStream desMergeStream = null;
BinaryWriter desMergeBinaryWriter = null;
FileStream srcStream = null;
BinaryReader srcReader = null;
try
{
filename = fileName + "*." + fileExtra;
arrFileNames = Directory.GetFiles(srcMerFilename, filename);
iFileCount = arrFileNames.Length;
filename = fileName.Substring(0, fileName.Length - 4);
progBarMerge.Maximum = iFileCount;
desMergeStream = new FileStream(txtMergDes.Text + "\\" + filename + "." + fileExtra, FileMode.OpenOrCreate);
desMergeBinaryWriter = new BinaryWriter(desMergeStream);
for (int i = 0; i < iFileCount; i++)
{
srcStream = new FileStream(arrFileNames[i], FileMode.Open);
srcReader = new BinaryReader(srcStream);
desMergeBinaryWriter.Write(srcReader.ReadBytes((int)srcStream.Length));
srcReader.Close();
srcStream.Close();
progBarMerge.Value = i + 1;
}
desMergeBinaryWriter.Close();
desMergeStream.Close();
MessageBox.Show("成功合并!");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
if (desMergeStream != null) desMergeStream.Close();
if (desMergeBinaryWriter != null) desMergeBinaryWriter.Close();
if (srcStream != null) srcStream.Close();
if (srcReader != null) srcReader.Close();
txtMerSrc.Clear();
txtMergDes.Clear();
srcMerFilename = null;
desMerFilename = null;
fileName = "";
fileExtra = "";
srcMerFilename = "";
progBarMerge.Value = 0;
}
}
}
}