Pho3nix
The Legend
Hi guys, was given the task of making a file splitting application. Now below is the code I have managed to scramble to use but I need it to split the files into files of 10megs and any remainder.
Here is my code
Here is my code
Code:
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;
namespace Merge_Split_App
{
public partial class Form1 : Form
{
public FileStream FS;
string mergeFolder;
public Form1()
{
InitializeComponent();
}
List<string> Packets = new List<string>();
//Merged file is stored in drive
string SaveFileFolder = @"c:\";
private void btn_browse_Click(object sender, EventArgs e)
{
openFileDialog1.ShowDialog();
txt_browse.Text = openFileDialog1.FileName;
}
public bool SplitFile(string SourceFile, int nNoofFiles)
{
bool split = false;
try
{
FileStream fs = new FileStream(SourceFile, FileMode.Open, FileAccess.Read);
int SizeofEachFile = (int)Math.Ceiling((double)fs.Length / nNoofFiles);
for (int i = 0; i < nNoofFiles; i++)
{
string baseFileName = Path.GetFileNameWithoutExtension(SourceFile);
string Extension = Path.GetExtension(SourceFile);
FileStream outputFile = new FileStream(Path.GetDirectoryName(SourceFile) + "\\" + baseFileName + "." +
i.ToString().PadLeft(5, Convert.ToChar("0")) + Extension + ".tmp", FileMode.Create, FileAccess.Write);
mergeFolder = Path.GetDirectoryName(SourceFile);
int bytesRead = 0;
byte[] buffer = new byte[SizeofEachFile];
if ((bytesRead = fs.Read(buffer, 0, SizeofEachFile)) > 0)
{
outputFile.Write(buffer, 0, bytesRead);
//outp.Write(buffer, 0, BytesRead);
string packet = baseFileName + "." + i.ToString().PadLeft(3, Convert.ToChar("0")) + Extension.ToString();
Packets.Add(packet);
}
outputFile.Close();
}
fs.Close();
}
catch (Exception Ex)
{
throw new ArgumentException(Ex.Message);
}
return split;
}
private void button1_Click(object sender, EventArgs e)
{
SplitFile(txt_browse.Text, Convert.ToInt32(5));
list1.Items.Add(Packets[0].ToString());
list1.Items.Add(Packets[1].ToString());
list1.Items.Add(Packets[2].ToString());
list1.Items.Add(Packets[3].ToString());
list1.Items.Add(Packets[4].ToString());
}
}
}