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 prjSplit
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void btnOpen_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
DialogResult dr = ofd.ShowDialog();
if (dr==DialogResult.Cancel)
{
return;
}
string fileName = ofd.FileName;
this.txtSourceFile.Text = fileName;
}
private void btnBrowser_Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd = new FolderBrowserDialog();
DialogResult dr = fbd.ShowDialog();
if (dr==DialogResult.Cancel)
{
return;
}
string path = fbd.SelectedPath;
this.txtFolder.Text = path;
}
private void btnStart_Click(object sender, EventArgs e)
{
string fileName = this.txtSourceFile.Text;
string s = this.txtSize.Text;
string path = this.txtFolder.Text;
if (!CheckFileName(fileName))
{
return;
}
if (!CheckSize(s))
{
return;
}
if (!CheckPath(path))
{
return;
}
SplitFile(fileName,int.Parse(s),path);
}
private void SplitFile(string fileName, int size, string path)
{
FileStream fs = new FileStream(fileName, FileMode.Open);
size = size * 1024;
byte[] bs = new byte[size];
int i=0;
while (true)
{
i++;
int x=fs.Read(bs, 0, bs.Length);
if (x==0)
{
break;
}
FileStream fsOut = new FileStream(path+"\\"+i.ToString()+".part",FileMode.Create);
fsOut.Write(bs, 0, x);
fsOut.Flush();
fsOut.Close();
}
fs.Close();
}
private bool CheckPath(string path)
{
if (path == "")
{
MessageBox.Show("没有选择目标文件夹。");
return false;
}
if (!Directory.Exists(path))
{
MessageBox.Show("目标文件夹不存在。");
return false;
}
return true;
}
private bool CheckSize(string s)
{
if (s.Trim()=="")
{
MessageBox.Show("大小必须填写。");
return false;
}
try
{
int.Parse(s);
}
catch (Exception)
{
MessageBox.Show("大小必须是数字。");
return false;
}
int size = int.Parse(s);
if (size<=0)
{
MessageBox.Show("大小必须是正数。");
return false;
}
return true;
}
private bool CheckFileName(string fileName)
{
if (fileName=="")
{
MessageBox.Show("没有选择源文件。");
return false;
}
if (!File.Exists(fileName))
{
MessageBox.Show("源文件不存在。");
return false;
}
return true;
}
}
}