背景:我正在使用C#开发一个WinForms应用程序,其中包含一个OpenFileDialog和FileBrowserDialog:
您如何建议在调试中修复以下任何错误:
这是我的代码:
private void sourceFiles_Click(object sender, EventArgs e)
{
Stream myStream;
int i = 0;
OpenFileDialog sourceFilesList = 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 = "Please Select Excel Source File(s) for Consolidation";
if (sourceFileOpenFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
if ((myStream = sourceFileOpenFileDialog.OpenFile()) != null)
{
using (myStream)
{
Log("Source Files: " + sourceFilesList.FileNames);
}
} // ends if
} // ends try
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
} // ends if (sourceFileOpenFileDialog.ShowDialog() == DialogResult.OK)
} // ends public void sourceFiles_Click
private void consolidateButton_Execute_Click(object sender, EventArgs e)
{
string consolidatedFolder = targetFolderBrowserDialog.SelectedPath;
foreach (String file in sourceFileOpenFileDialog.FileNames)
{
try
{
// Copy each selected xlsx files into the specified TargetFolder
System.IO.File.Copy(sourceFileOpenFileDialog.FileName, consolidatedFolder + @"\" + System.IO.Path.GetFileName(sourceFileOpenFileDialog.FileName));
Log("File" + sourceFileOpenFileDialog.FileName + " has been copied to " + consolidatedFolder + @"\" + System.IO.Path.GetFileName(sourceFileOpenFileDialog.FileName));
}
} // ends foreach loop
} // ends void consolidateButton_Execute_Click
我会给任何有用的答案+1票! 谢谢你的期待!
更新:更新了代码w / foreach(sourceFilesList.FileNames中的字符串FileName)循环和一个列表框控件,仍有问题w / filebrowser加载2x,以及“Source Files:System.String []”消息
答案 0 :(得分:1)
要仅从文件路径获取文件名,请使用Path.GetFileName(...)。
要检查是否选择了多个文件,您只需检查openFileDialog.FileNames Length属性 - 它是一个数组。
答案 1 :(得分:1)
您的代码段与您的问题不太匹配,没有显示FolderBrowserDialog的迹象。 File.Copy()调用中存在明显错误,您传递sourceFileOpenFileDialog.FileName而不是 file 。
检查this answer以查看在有限空间内显示路径名的方法:
using System;
using System.ComponentModel;
using System.Windows.Forms;
class PathLabel : Label
{
[Browsable(false)]
public override bool AutoSize
{
get { return base.AutoSize; }
set { base.AutoSize = false; }
}
protected override void OnPaint(PaintEventArgs e)
{
TextFormatFlags flags = TextFormatFlags.Left | TextFormatFlags.PathEllipsis;
TextRenderer.DrawText(e.Graphics, this.Text, this.Font, this.ClientRectangle, this.ForeColor, flags);
}
}
答案 2 :(得分:0)
修复了日志记录窗口消息:“源文件:System.String []”,添加:
foreach (string FileName in sourceFilesList.FileNames)
{
sourceFilesList.FileNames[i] = FileName;
listBoxSourceFiles.Items.Add(FileName);
Log("Source Files: " + sourceFilesList.FileNames[i]);
i++;
}
// under if ((myStream = sourceFileOpenFileDialog.OpenFile()) != null)
修复了以下选择文件时出现的2个FileBrowserDialog框:
if ((myStream = sourceFilesList.OpenFile()) != null)
// deleted duplicate line