我目前有一个根据用户选择合并单词文档的程序。目前,该程序允许通过按下按钮选择总共5个文件进行合并,如下所示:
// combine files in folder selected using MsWord.cs,
private void combineButton2_Click(object sender, EventArgs e)
{
loadingForm.Show(); // displays 'loading' form
// 5 documents selected by the user
selectedDocs = new []
{
selectedFile1,
selectedFile2,
selectedFile3,
selectedFile4,
selectedFile5
};
string fileDate = DateTime.Now.ToString("dd-MM-yy");
string fileTime = DateTime.Now.ToString("HH.mm.ss");
// output folder user selects
string outcomeFolder2 = outputFolder2;
string outputFile2 = "Combined Files " + fileDate + " @ " + fileTime + ".docx";
string outputFileName2 = Path.Combine(outcomeFolder2, outputFile2);
MsWord.Merge(selectedDocs, outputFileName2, true);
loadingForm.Hide(); // to hide the form
// Message displaying how many files are combined.
MessageBox.Show("A total of " + selectedDocs.Length.ToString() + " documents have been merged", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
当前的问题是,用户必须选择5个文件。如果他们选择2/3/4文件的程序错误,这限制了整体使用。我正在阅读Switch语句,这可能有助于纠正这种情况,但我对命令并不了解。我假设它会像:
switch (merge)
{
case 'selectedFile1':
selectedDocs = new [selectedFile1]
break;
case 'selectedFile2':
selectedDocs = new [selectedFile1, selectedFile2]
break;
case 'selectedFile3':
selectedDocs = new [selectedFile1, selectedFile2, selectedFile3]
break;
case 'selectedFile4':
selectedDocs = new [selectedFile1, selectedFile2, selectedFile3, selectedFile4]
break;
case 'selectedFile5':
selectedDocs = new [selectedFile1, selectedFile2, selectedFile3, selectedFile4, selectedFile5]
break;
default:
MessageBox.Show("error, no files selected");
break;
}
**更新信息**
// input Destination - for separate files
private string[] selectedDocs;
// input file 1
string selectedFile1;
private void browseFileButton1_Click(object sender, EventArgs e)
{
OpenFileDialog openFileInput1 = new OpenFileDialog();
openFileInput1.Filter = "Word Documents|*.docx;*.doc";
openFileInput1.Title = "Select a Word Document";
// Default file, altered when the user selects file of choice
openFileInput1.FileName = selectedFile1;
// initial file path display
filePath1.Text = openFileInput1.FileName;
// 'OK' button being confirmed on the popup menu
if (openFileInput1.ShowDialog() == DialogResult.OK)
{
selectedFile1 = openFileInput1.FileName;
filePath1.Text = openFileInput1.FileName;
}
}
// input file 2
string selectedFile2;
private void browseFileButton2_Click(object sender, EventArgs e)
{
OpenFileDialog openFileInput2 = new OpenFileDialog();
openFileInput2.Filter = "Word Documents|*.docx;*.doc";
openFileInput2.Title = "Select a Word Document";
// Default file, altered when the user selects file of choice
openFileInput2.FileName = selectedFile2;
// initial file path display
filePath2.Text = openFileInput2.FileName;
// 'OK' button being confirmed on the popup menu
if (openFileInput2.ShowDialog() == DialogResult.OK)
{
selectedFile2 = openFileInput2.FileName;
filePath2.Text = openFileInput2.FileName;
}
}
// input file 3
private string selectedFile3;
private void browseFileButton3_Click(object sender, EventArgs e)
{
OpenFileDialog openFileInput3 = new OpenFileDialog();
openFileInput3.Filter = "Word Documents|*.docx;*.doc";
openFileInput3.Title = "Select a Word Document";
// Default file, altered when the user selects file of choice
openFileInput3.FileName = selectedFile3;
// initial file path display
filePath3.Text = openFileInput3.FileName;
// 'OK' button being confirmed on the popup menu
if (openFileInput3.ShowDialog() == DialogResult.OK)
{
selectedFile3 = openFileInput3.FileName;
filePath3.Text = openFileInput3.FileName;
}
}
// input file 4
private string selectedFile4;
private void browseFileButton4_Click(object sender, EventArgs e)
{
OpenFileDialog openFileInput4 = new OpenFileDialog();
openFileInput4.Filter = "Word Documents|*.docx;*.doc";
openFileInput4.Title = "Select a Word Document";
// Default file, altered when the user selects file of choice
openFileInput4.FileName = selectedFile4;
// initial file path display
filePath4.Text = openFileInput4.FileName;
// 'OK' button being confirmed on the popup menu
if (openFileInput4.ShowDialog() == DialogResult.OK)
{
selectedFile4 = openFileInput4.FileName;
filePath4.Text = openFileInput4.FileName;
}
}
// input file 5
private string selectedFile5;
private void browseFileButton5_Click(object sender, EventArgs e)
{
OpenFileDialog openFileInput5 = new OpenFileDialog();
openFileInput5.Filter = "Word Documents|*.docx;*.doc";
openFileInput5.Title = "Select a Word Document";
// Default file, altered when the user selects file of choice
openFileInput5.FileName = selectedFile5;
// initial file path display
filePath5.Text = openFileInput5.FileName;
// 'OK' button being confirmed on the popup menu
if (openFileInput5.ShowDialog() == DialogResult.OK)
{
selectedFile5 = openFileInput5.FileName;
filePath5.Text = openFileInput5.FileName;
}
}
// Output Destination - for separate files
private string outputFolder2 = @"";
private void browseButtonOut2_Click(object sender, EventArgs e)
{
FolderBrowserDialog diagBrowserOutput2 = new FolderBrowserDialog();
diagBrowserOutput2.Description = "Select a folder location to save the document...";
// Default folder, altered when the user selects folder of choice
diagBrowserOutput2.SelectedPath = outputFolder2;
// Output file path display
outputPath2.Text = diagBrowserOutput2.SelectedPath;
if (DialogResult.OK == diagBrowserOutput2.ShowDialog())
{
outputFolder2 = diagBrowserOutput2.SelectedPath;
outputPath2.Text = diagBrowserOutput2.SelectedPath;
}
}
// combine files in folder selected using MsWord.cs,
private void combineButton2_Click(object sender, EventArgs e)
{
loadingForm.Show(); // To show the form
selectedDocs = new []
{
selectedFile1,
selectedFile2,
selectedFile3,
selectedFile4,
selectedFile5
};
string fileDate = DateTime.Now.ToString("dd-MM-yy");
string fileTime = DateTime.Now.ToString("HH.mm.ss");
string outcomeFolder2 = outputFolder2;
string outputFile2 = "Combined Files " + fileDate + " @ " + fileTime + ".docx";
string outputFileName2 = Path.Combine(outcomeFolder2, outputFile2);
MsWord.Merge(selectedDocs, outputFileName2, true);
loadingForm.Hide(); // to hide the form
// Message displaying how many files are combined.
MessageBox.Show("A total of " + selectedDocs.Length.ToString() + " documents have been merged", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
答案 0 :(得分:1)
您可以使用List<string>
代替并在最后一刻转换为数组,因此它始终是完全正确的大小。
List<string> doclist = new List<string>();
doclist.Add(selectedFile1);
//...etc.
//Then...
MsWord.Merge(doclist.ToArray(), outputFileName2, true);