Skydiver, on 04 October 2012 - 12:54 AM, said:
What is the exact error, and an which line is it being reported on.
Could you also post your real code. The code you posted above will only call MainForm() if there are no command line parameters, yet you are passing in args[0]. You should have gotten an IndexOutOfRangeException if that were your real code.
Could you also post your real code. The code you posted above will only call MainForm() if there are no command line parameters, yet you are passing in args[0]. You should have gotten an IndexOutOfRangeException if that were your real code.
program.cs
static void Main(string[] args)
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
if (args.Length == 0)
{
Application.Run(new MainForm());
}
else
{
Application.Run(new MainForm2(args));
}
}
MainForm2.cs
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.IO.Compression;
using Ionic.Zip;
using Ionic.Zlib;
namespace FileCompressionandDecompression
{
public partial class MainForm2 : Form
{
public MainForm2(string[] args)
{
string filename = Path.GetFullPath(args[0]);
string filePath = Path.GetFileName(args[0]);
InitializeComponent();
DataTable pldtable = new DataTable();
//set datagridview columns
pldtable.Columns.Add("Full Name");
pldtable.Columns.Add("File Name");
pldtable.Columns.Add("Compress Size");
pldtable.Columns.Add("Readable Size");
pldtable.Columns.Add("Date Modified");
pldtable.Columns.Add("Uncompress Size");
pldtable.Columns.Add("Type");
if (args.Length == 0)
{
MessageBox.Show("File don't Exist", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
//enable compress button
//compress files and put it to datagridview
}
else
{
tb.Text = filename;
openStatus.Visible = true;
using (ZipFile pld = ZipFile.Read(filePath))
{
foreach (ZipEntry en in pld)
{
openStatus.Text = string.Format("Reading: {0}", en.FileName);
openStatus.Update();
string fullFileName = en.FileName;
string extension;
string fileName;
extension = Path.GetExtension(fullFileName);
fileName = Path.GetFileName(fullFileName);
FileInfo fileInfo = new FileInfo(fullFileName);
string fileType = fileInfo.Name;
DirectoryInfo dir = Directory.GetParent(fullFileName);
string folderName = dir.Parent + "//" + fileName;
//set bytes size
string sizeReadable = en.CompressedSize.ToString() + " bytes";
string resultDecomp = en.UncompressedSize.ToString() + " bytes";
//add filename to rows
pldtable.Rows.Add(en.FileName, fileType, sizeReadable, en.CompressedSize, en.LastModified, resultDecomp, extension);
dgv.DataSource = pldtable;
dgv.Columns[3].Visible = false;
dgv.Columns[0].Visible = false;
dgv.Columns[1].Width = 150;
dgv.Columns[2].Width = 120;
dgv.Columns[4].Width = 120;
dgv.Columns[5].Width = 130;
dgv.Columns[6].Width = 63;
if (dgv.Rows.Count > 1)
{
int total = 0;
for (int i = 0; i < dgv.RowCount; i++)
{
total += Convert.ToInt32(dgv.Rows[i].Cells[3].Value);
totalSize.Text = total.ToString() + " bytes";
totalFiles.Text = dgv.Rows[i].Index.ToString() + " files";
}
}
}
}
}
dgv.Sort(dgv.Columns[3], ListSortDirection.Descending);
openStatus.Visible = false;
appStat.Text = "Success opening file.";
}
public static TextBox tb = new TextBox();
public static DataGridView dgv = new DataGridView();
public static ToolStripButton removebtn = new ToolStripButton();
public static ToolStripButton decompbtn = new ToolStripButton();
public static ToolStripButton decompbtnHere = new ToolStripButton();
public static ToolStripLabel appStat = new ToolStripLabel();
public static Label totalFiles = new Label();
public static Label totalSize = new Label();
public static Label labelSize = new Label();
public static Label openStatus = new Label();
private void openToolStripMenuItem_Click(object sender, EventArgs e)
{
openFileDialog1.Title = "Open File";
openFileDialog1.Filter = "Compressed Files (*.pld,*.zip)|*.pld;*.zip";
openFileDialog1.FileName = "";
if (openFileDialog1.ShowDialog() == DialogResult.Cancel)
{
return;
}
string sFilePath;
sFilePath = openFileDialog1.FileName;
string path = sFilePath;
DataTable pldtable = new DataTable();
//set datagridview columns
pldtable.Columns.Add("Full Name");
pldtable.Columns.Add("File Name");
pldtable.Columns.Add("Compress Size");
pldtable.Columns.Add("Readable Size");
pldtable.Columns.Add("Date Modified");
pldtable.Columns.Add("Uncompress Size");
pldtable.Columns.Add("Type");
if (sFilePath == "")
return;
if (System.IO.File.Exists(sFilePath) == false)
{
return;
}
else
{
textBox1.Text = sFilePath;
label5.Visible = true;
}
//enable compress button
toolStripButton4.Enabled = true;
toolStripButton2.Enabled = true;
toolStripButton6.Enabled = true;
//compress files and put it to datagridview
using (ZipFile pld = ZipFile.Read(sFilePath))
{
foreach (ZipEntry en in pld)
{
label5.Text = string.Format("Reading: {0}", en.FileName);
label5.Update();
string fullFileName = en.FileName;
string extension;
string fileName;
extension = Path.GetExtension(fullFileName);
fileName = Path.GetFileName(fullFileName);
FileInfo fileInfo = new FileInfo(fullFileName);
string fileType = fileInfo.Name;
DirectoryInfo dir = Directory.GetParent(fullFileName);
string folderName = dir.Parent + "//" + fileName;
// resultPath = Path.GetDirectoryName(fullFileName);
//set bytes size
string sizeReadable = en.CompressedSize.ToString() + " bytes";
string resultDecomp = en.UncompressedSize.ToString() + " bytes";
//add filename to rows
pldtable.Rows.Add(en.FileName, fileType, sizeReadable, en.CompressedSize, en.LastModified, resultDecomp, extension);
dataGridView1.DataSource = pldtable;
dataGridView1.Columns[3].Visible = false;
dataGridView1.Columns[0].Visible = false;
dataGridView1.Columns[1].Width = 150;
dataGridView1.Columns[2].Width = 120;
dataGridView1.Columns[4].Width = 120;
dataGridView1.Columns[5].Width = 130;
dataGridView1.Columns[6].Width = 63;
if (dataGridView1.Rows.Count > 1)
{
int total = 0;
for (int i = 0; i < dataGridView1.RowCount; i++)
{
total += Convert.ToInt32(dataGridView1.Rows[i].Cells[3].Value);
TotalSize.Text = total.ToString() + " bytes";
TotalFiles.Text = dataGridView1.Rows[i].Index.ToString() + " files";
}
}
}
}
dataGridView1.Sort(dataGridView1.Columns[3], ListSortDirection.Descending);
label5.Visible = false;
appStatus.Text = "Success opening file";
}
private void toolStripButton1_Click(object sender, EventArgs e)
{
AddFile2 addFile = new AddFile2();
addFile.ShowDialog();
}
private void toolStripButton5_Click(object sender, EventArgs e)
{
openFileDialog1.Title = "Open File";
openFileDialog1.Filter = "Compressed Files (*.pld,*.zip)|*.pld;*.zip";
openFileDialog1.FileName = "";
if (openFileDialog1.ShowDialog() == DialogResult.Cancel)
{
return;
}
string sFilePath;
sFilePath = openFileDialog1.FileName;
string path = sFilePath;
DataTable pldtable = new DataTable();
//set datagridview columns
pldtable.Columns.Add("Full Name");
pldtable.Columns.Add("File Name");
pldtable.Columns.Add("Compress Size");
pldtable.Columns.Add("Readable Size");
pldtable.Columns.Add("Date Modified");
pldtable.Columns.Add("Uncompress Size");
pldtable.Columns.Add("Type");
if (sFilePath == "")
return;
if (System.IO.File.Exists(sFilePath) == false)
{
return;
}
else
{
textBox1.Text = sFilePath;
label5.Visible = true;
}
//enable compress button
toolStripButton4.Enabled = true;
toolStripButton2.Enabled = true;
toolStripButton6.Enabled = true;
//compress files and put it to datagridview
using (ZipFile pld = ZipFile.Read(sFilePath))
{
foreach (ZipEntry en in pld)
{
label5.Text = string.Format("Reading: {0}", en.FileName);
label5.Update();
string fullFileName = en.FileName;
string extension;
string fileName;
extension = Path.GetExtension(fullFileName);
fileName = Path.GetFileName(fullFileName);
FileInfo fileInfo = new FileInfo(fullFileName);
string fileType = fileInfo.Name;
DirectoryInfo dir = Directory.GetParent(fullFileName);
string folderName = dir.Parent + "//" + fileName;
//resultPath = Path.GetDirectoryName(fullFileName);
//set bytes size
string sizeReadable = en.CompressedSize.ToString() + " bytes";
string resultDecomp = en.UncompressedSize.ToString() + " bytes";
//add filename to rows
pldtable.Rows.Add(en.FileName, fileType, sizeReadable, en.CompressedSize, en.LastModified, resultDecomp, extension);
dataGridView1.DataSource = pldtable;
dataGridView1.Columns[3].Visible = false;
dataGridView1.Columns[0].Visible = false;
dataGridView1.Columns[1].Width = 150;
dataGridView1.Columns[2].Width = 120;
dataGridView1.Columns[4].Width = 120;
dataGridView1.Columns[5].Width = 130;
dataGridView1.Columns[6].Width = 63;
if (dataGridView1.Rows.Count > 1)
{
int total = 0;
for (int i = 0; i < dataGridView1.RowCount; i++)
{
total += Convert.ToInt32(dataGridView1.Rows[i].Cells[3].Value);
TotalSize.Text = total.ToString() + " bytes";
TotalFiles.Text = dataGridView1.Rows[i].Index.ToString() + " files";
}
}
}
}
dataGridView1.Sort(dataGridView1.Columns[3], ListSortDirection.Descending);
label5.Visible = false;
appStatus.Text = "Success opening file.";
}
private void toolStripButton4_Click(object sender, EventArgs e)
{
//decompress files with specific directory
using (ZipFile pld = new ZipFile())
{
string pldTodecomp = textBox1.Text;
string pathFile = pldTodecomp;
string fileDir;
fileDir = Path.GetDirectoryName(pathFile);
using (ZipFile pld2 = ZipFile.Read(pldTodecomp))
{
foreach (ZipEntry en in pld2)
{
en.Extract(fileDir,
ExtractExistingFileAction.OverwriteSilently);
}
}
MessageBox.Show("Decompress file is complete", "Complete", MessageBoxButtons.OK, MessageBoxIcon.Information);
textBox1.Text = string.Empty;
dataGridView1.DataSource = string.Empty;
toolStripButton4.Enabled = false;
toolStripButton2.Enabled = false;
toolStripButton6.Enabled = false;
appStatus.Text = "Ready";
TotalSize.Text = "0";
TotalFiles.Text = "0";
}
}
private void toolStripButton2_Click(object sender, EventArgs e)
{
DecompTo2 DecompFrm = new DecompTo2(this);
DecompFrm.ShowDialog();
}
private void MainForm2_Load(object sender, EventArgs e)
{
toolStripButton4.Enabled = false;
toolStripButton2.Enabled = false;
toolStripButton6.Enabled = false;
tb = textBox1;
dgv = dataGridView1;
removebtn = toolStripButton6;
decompbtn = toolStripButton4;
decompbtnHere = toolStripButton2;
appStat = appStatus;
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
totalFiles = TotalFiles;
totalSize = TotalSize;
labelSize = label2;
openStatus = label5;
}
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Dispose();
}
private void toolStripButton6_Click(object sender, EventArgs e)
{
string pathFile = textBox1.Text;
if (this.dataGridView1.SelectedRows.Count > 0)
{
DialogResult conDelete = MessageBox.Show("Are you sure you want to delete file?", "Delete",
MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (conDelete == DialogResult.Yes)
{
string toBeRemove = (dataGridView1.SelectedCells[0].Value.ToString());
using (ZipFile pld = ZipFile.Read(pathFile))
{
pld.RemoveEntry(toBeRemove);
pld.Save();
}
dataGridView1.Rows.Remove(dataGridView1.CurrentRow);
if (dataGridView1.Rows.Count > 1)
{
int total = 0;
for (int i = 0; i < dataGridView1.RowCount; i++)
{
total += Convert.ToInt32(dataGridView1.Rows[i].Cells[3].Value);
TotalSize.Text = total.ToString() + " bytes";
TotalFiles.Text = dataGridView1.Rows[i].Index.ToString() + " files";
}
}
}
else if (conDelete == DialogResult.No)
{
return;
}
}
}
}
}

New Topic/Question
Reply




MultiQuote



|