C#-StreamReader安全异常处理WinForms应用程序上与OpenFileDialog相关的错误
大家好,背景: 我正在使用C#开发一个WinForms应用程序,它带有一个OpenFileDialog,FileBrowserDialog,可以使用命令行可执行文件将多个文件从excel转换为1个csv文件 错误:我应该使用什么指令或程序集引用来防止这些错误C#-StreamReader安全异常处理WinForms应用程序上与OpenFileDialog相关的错误,c#,winforms,error-handling,streamreader,openfiledialog,C#,Winforms,Error Handling,Streamreader,Openfiledialog,大家好,背景: 我正在使用C#开发一个WinForms应用程序,它带有一个OpenFileDialog,FileBrowserDialog,可以使用命令行可执行文件将多个文件从excel转换为1个csv文件 错误:我应该使用什么指令或程序集引用来防止这些错误 当前上下文中不存在“sOut” 当前上下文中不存在“sErr” “sourceFileOpenFileDialog.SelectedFiles”不包含“SelectedFiles”的定义,并且找不到接受system.windows.for
- 当前上下文中不存在“sOut”
- 当前上下文中不存在“sErr”
- “sourceFileOpenFileDialog.SelectedFiles”不包含“SelectedFiles”的定义,并且找不到接受system.windows.forms.openfiledialog类型的第一个参数的扩展方法(是否缺少using指令或程序集引用?)
- “sourceFileOpenFileDialog.SelectedPath”不包含“SelectedPath”的定义,并且找不到接受system.windows.forms.openfiledialog类型的第一个参数的扩展方法(是否缺少using指令或程序集引用?)
- 找不到“SecurityException”(是否缺少using指令或程序集引用?)
- 找不到“进程”(是否缺少using指令或程序集引用?)
- 找不到“文件名”(是否缺少using指令或程序集引用?)
- “sourceFileOpenFileDialog.FileNames”无法将类型“string[]”隐式转换为“string”
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
解决你的错误
sourceFileOpenFileDialog.FileName无法将类型“string[]”隐式转换为“string”
您需要更改此部分,您正在尝试将字符串数组推入字符串(.Text)
也许迭代文件名数组并将字符串连接在一起以放入.Text
对于“.SelectedFiles”和“.SelectedPath”的问题,这些不是openFileDialog的属性,因此它会抱怨
同样,您可以使用“.FileName”获取对话框中选择的文件(如果只允许一个选择,则使用“.FileName”)
使用sOut和sErr时,您会在流程的后期设置它们,并在您的最终声明中清理它们,如果流程在您到达目标之前爆炸,可能会发生什么情况
StreamReader sOut = covertFilesProcess.StandardOutput;
StreamReader sErr = covertFilesProcess.StandardError;
然后,当您点击finally时,它不知道要关闭的sOut和sErr是什么,因为它们从未创建过。修复了以下错误:
- 通过添加,找不到“SecurityException”(是否缺少using指令或程序集引用?)
using System.Security;
- 无法通过添加以下内容找到“进程”(是否缺少using指令或程序集引用?)
using System.Diagnostics;
- 通过更改为sourceFileOpenFileDialog.fileName,找不到“文件名”(是否缺少using指令或程序集引用?)
System.IO.File.Copy(sourceFileOpenFileDialog.FileName, consolidatedFolder + @"\" + System.IO.Path.GetFileName(sourceFileOpenFileDialog.FileName));
textBoxSourceFiles.Text = sourceFileOpenFileDialog.FileName;
- “sourceFileOpenFileDialog.FileNames”无法通过更改为sourceFileOpenFileDialog.FileName将类型“string[]”隐式转换为“string”
System.IO.File.Copy(sourceFileOpenFileDialog.FileName, consolidatedFolder + @"\" + System.IO.Path.GetFileName(sourceFileOpenFileDialog.FileName));
textBoxSourceFiles.Text = sourceFileOpenFileDialog.FileName;
- “sOut”和“sErr”在当前上下文中不存在,通过如下更改结束语句的位置进行修复。我不确定这是否会打乱异常捕获程序,但我们将看到:
StreamReader sOut = consolidateFilesProcess.StandardOutput; StreamReader sErr = consolidateFilesProcess.StandardError; sOut.Close(); sErr.Close();
- “sourceFileOpenFileDialog.SelectedFiles”不包含“SelectedFiles”的定义,并且找不到接受system.windows.forms.openfiledialog类型的第一个参数的扩展方法(是否缺少using指令或程序集引用?)。通过将SelectedFiles更改为文件名修复:
// Text box value declarataions private void textBoxSourceFiles_TextChanged(object sender, EventArgs e) { sourceFileOpenFileDialog.FileName = textBoxSourceFiles.Text; }
if (sourceFileOpenFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = sourceFileOpenFileDialog.OpenFile()) != null)
{
using (myStream)
{
textBoxSourceFiles.Text = sourceFileOpenFileDialog.FileName;
}
}
}
修复错误:找不到“进程”(是否缺少using指令或程序集引用?);已修复错误:找不到“SecurityException”(是否缺少using指令或程序集引用?)w/正在使用System.Security;修复了更改为sourceFileOpenFileDialog时找不到“文件名”(是否缺少using指令或程序集引用?)。文件名我们将再给您一个小时左右的时间。来吧,hans solo,我不想花几个小时调试,谢谢您的回复!您能否编辑您的答案以包含正确的代码?我应该使用哪些正确的属性来引用在openFileDialog中选择的路径和文件?如何修复代码中的sOut进程……我应该使用以下字符串directoryPath=Path.GetDirectoryName(filePath)?