Hey Everyone, Background: I'm developing a WinForms application using C# with an OpenFileDialog, FileBrowserDialog that will convert multiple files from excel to 1 csv file using a command line executable.
Errors: What using directives or assembly references should I add to prevent these errors??
Thanks!
Here's the code from the MainForm.CS file:
using System;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;
using System.Diagnostics;
using System.Security;
// Select Source Files Button
private void sourceFiles_Click(object sender, EventArgs e)
{
Stream myStream = null;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
this.sourceFileOpenFileDialog.InitialDirectory = "i:\\CommissisionReconciliation\\Review\\";
this.sourceFileOpenFileDialog.Filter = "Excel Files (*.xls;*.xlsx;)|*.xls;*.xlsx;|All Files (*.*)|*.*";
this.sourceFileOpenFileDialog.FilterIndex = 2;
this.sourceFileOpenFileDialog.RestoreDirectory = true;
this.sourceFileOpenFileDialog.Multiselect = true;
this.sourceFileOpenFileDialog.Title = "Excel File Browser";
DialogResult dr = this.sourceFileOpenFileDialog.ShowDialog();
if (dr == System.Windows.Forms.DialogResult.OK)
{
string consolidatedFolder = targetFolderBrowserDialog.SelectedPath;
// Read the files
foreach (String file in sourceFileOpenFileDialog.FileNames)
{
try
{
// Copy each selected xlsx files into the specified TargetFolder
System.IO.File.Copy(fileName, consolidatedFolder + @"\" + System.IO.Path.GetFileName(fileName));
// Convert each selected XLSX File to CSV Using the command prompt
// [I:\CommissisionReconciliation\App\ConvertExcel\ConvertExcelTo.exe ^ .XLS file location ^ filename.csv]
// example: ConvertExcelTo.exe ^ I:\CommissisionReconciliation\ Review\_Consolidated\ALH\2011-350-00-600070-
// 03-09alh-AMLHS of Florida.xlsx ^ 2011-350-00-600070-03-09alh-AMLHS of Florida.csv
Process convertFilesProcess = new Process();
// command prompt execution
convertFilesProcess.StartInfo.WorkingDirectory = "I:\\CommissisionReconciliation\\App\\ConvertExcel\\";
convertFilesProcess.StartInfo.FileName = "ConvertExcelTo.exe";
convertFilesProcess.StartInfo.Arguments = " ^ " + targetFolderBrowserDialog.SelectedPath + "^" + csvFileName + ".csv";
convertFilesProcess.StartInfo.UseShellExecute = true;
convertFilesProcess.StartInfo.CreateNoWindow = true;
convertFilesProcess.StartInfo.RedirectStandardOutput = true;
convertFilesProcess.StartInfo.RedirectStandardError = true;
convertFilesProcess.Start();
StreamReader sOut = convertFilesProcess.StandardOutput;
StreamReader sErr = convertFilesProcess.StandardError;
}
catch (SecurityException ex)
{
// The user lacks appropriate permissions to read files, discover paths, etc.
MessageBox.Show("Security error. The user lacks appropriate permissions to read files, discover paths, etc. Please contact your administrator for details.\n\n" +
"Error message: " + ex.Message + "\n\n" +);
}
catch (Exception ex)
{
// Could not load the image - probably related to Windows file system permissions.
MessageBox.Show("Cannot display the image: " + file.Substring(file.LastIndexOf('\\'))
+ ". You may not have permission to read the file, or " +
"it may be corrupt.\n\nReported error: " + ex.Message);
}
finally
{
Stream sOut
.Close();
sErr.Close();
}
try
{
// Combine all .csv files into 1 csv file using the command prompt
// [.csv File location: Copy *.csv ^ filename.csv]
// example: [.CSV I:\CommissisionReconciliation\ Review\_Consolidated\ALH\: Copy *.csv
// ^2011-350-00-600070-03-09alh-AMLHS of Florida.csv)
Process consolidateFilesProcess = new Process();
// substring function to take off the extension from sourceFileOpenFileDialog.FileName
// int csvFileName.Length = sourceFileOpenFileDialog.FileName.Length - 3;
consolidateFilesProcess.StartInfo.WorkingDirectory = "I:\\CommissisionReconciliation\\App\\ConvertExcel\\";
consolidateFilesProcess.StartInfo.FileName = "ConvertExcelTo.exe";
consolidateFilesProcess.StartInfo.Arguments = " .CSV " + " ^ " + targetFolderBrowserDialog.SelectedPath + ": Copy *.csv ^" + csvFileName+ ".csv";
consolidateFilesProcess.StartInfo.UseShellExecute = false;
consolidateFilesProcess.StartInfo.CreateNoWindow = true;
consolidateFilesProcess.StartInfo.RedirectStandardOutput = true;
consolidateFilesProcess.StartInfo.RedirectStandardError = true;
consolidateFilesProcess.Start();
StreamReader sOut = consolidateFilesProcess.StandardOutput;
StreamReader sErr = consolidateFilesProcess.StandardError;
}
catch (SecurityException ex)
{
// The user lacks appropriate permissions to read files, discover paths, etc.
MessageBox.Show("Security error. The user lacks appropriate permissions to read files, discover paths, etc. Please contact your administrator for details.\n\n" +
"Error message: " + ex.Message + "\n\n" +);
}
catch (Exception ex)
{
// Could not load the image - probably related to Windows file system permissions.
MessageBox.Show("Cannot display the image: " + file.Substring(file.LastIndexOf('\\'))
+ ". You may not have permission to read the file, or " +
"it may be corrupt.\n\nReported error: " + ex.Message);
}
finally
{
sOut.Close();
sErr.Close();
}
} // ends foreach (String file in openFileDialog1.FileNames)
} // ends if (dr == System.Windows.Forms.DialogResult.OK)
if (sourceFileOpenFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = sourceFileOpenFileDialog.OpenFile()) != null)
{
using (myStream)
{
textBoxSourceFiles.Text = sourceFileOpenFileDialog.FileNames;
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
if (sourceFileOpenFileDialog.ShowDialog() == DialogResult.OK)
{
Log("Source Files: " + sourceFileOpenFileDialog.SelectedFiles);
}
textBoxSourceFiles.Text = sourceFileOpenFileDialog.SelectedFiles;
} // ends selectFilesButton_Click method
// Source Folder Path Click Button
Fixed the following Errors so far:
'SecurityException' could not be found (are you missing a using directive or assembly reference?) by adding
using System.Security;
'Process' could not be found (are you missing a using directive or assembly reference?) by adding:
using System.Diagnostics;
'fileName' could not be found (are you missing a using directive or assembly reference?) by changing to sourceFileOpenFileDialog.FileName
System.IO.File.Copy(sourceFileOpenFileDialog.FileName, consolidatedFolder + @"\" + System.IO.Path.GetFileName(sourceFileOpenFileDialog.FileName));
'sourceFileOpenFileDialog.FileNames' cannot implicitly convert type 'string[]' to 'string' by changing to sourceFileOpenFileDialog.FileName
textBoxSourceFiles.Text = sourceFileOpenFileDialog.FileName;
'sOut' & 'sErr' does not exist in the current context fixed by changing placement of the close statements as follows. I'm not sure if this messes up the exception catcher or not but we will see:
StreamReader sOut = consolidateFilesProcess.StandardOutput;
StreamReader sErr = consolidateFilesProcess.StandardError;
sOut.Close();
sErr.Close();
'sourceFileOpenFileDialog.SelectedFiles' does not contain a definition for 'SelectedFiles' and no extension method accepting a first arg of type system.windows.forms.openfiledialog could be found (are you missing a using directive or assembly reference?). Fixed by changing SelectedFiles to FileNames:
// Text box value declarataions
private void textBoxSourceFiles_TextChanged(object sender, EventArgs e)
{
sourceFileOpenFileDialog.FileName = textBoxSourceFiles.Text;
}
'sourceFileOpenFileDialog.SelectedPath' does not contain a definition for 'SelectedPath' and no extension method accepting a first arg of type system.windows.forms.openfiledialog could be found (are you missing a using directive or assembly reference?). Fixed by changing SelectedPath to FileName:
if (sourceFileOpenFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = sourceFileOpenFileDialog.OpenFile()) != null)
{
using (myStream)
{
textBoxSourceFiles.Text = sourceFileOpenFileDialog.FileName;
}
}
}