保存文件时输出位置问题

时间:2016-03-18 13:53:21

标签: c#

目前在保存合并字时遇到问题。使用文件浏览器对话框将doc发送到特定位置

    // input destintion
    private string[] sourceFiles;
    private void browseButton_Click(object sender, EventArgs e)
    {
        FolderBrowserDialog diagBrowser = new FolderBrowserDialog();
        diagBrowser.Description = "Select a folder which contains files needing combined...";

        // Default folder, altered when the user selects folder of choice 
        string selectedFolder = @"";
        diagBrowser.SelectedPath = selectedFolder;

        // initial file path display
        folderPath.Text = diagBrowser.SelectedPath;

        if (DialogResult.OK == diagBrowser.ShowDialog())
        {
            // Grab the folder that was chosen
            selectedFolder = diagBrowser.SelectedPath;
            folderPath.Text = diagBrowser.SelectedPath;

            sourceFiles = Directory.GetFiles(selectedFolder, "*.doc");
        }
    }

    // output destintion
    private string[] sourceFileOutput;
    private void browseButtonOut_Click(object sender, EventArgs e)
    {
        FolderBrowserDialog diagBrowserOutput = new FolderBrowserDialog();
        diagBrowserOutput.Description = "Select a folder location to save the document...";

        // Default folder, altered when the user selects folder of choice 
        string outputFolder = @"";
        diagBrowserOutput.SelectedPath = outputFolder;

        // output file path display
        outputPath.Text = diagBrowserOutput.SelectedPath;

        if (DialogResult.OK == diagBrowserOutput.ShowDialog())
        {
            outputFolder = diagBrowserOutput.SelectedPath;
            outputPath.Text = diagBrowserOutput.SelectedPath;

            sourceFileOutput = Directory.GetFiles(outputFolder);
        }
    }

    private void combineButton_Click(object sender, EventArgs e)
    {
        if (sourceFiles != null && sourceFiles.Length > 0)
        {
            string outputFileName = (sourceFileOutput + "Combined.docx");
            MsWord.Merge(sourceFiles, outputFileName, true);

            // Message displaying how many files are combined. 
            MessageBox.Show("A total of " + sourceFiles.Length.ToString() + " documents have been merged", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        else
        {
            // Message displaying error.
            MessageBox.Show("Please a select a relevant folder with documents to combine", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

而不是得到'combined.docx'在选择的位置,我得到一个名为' System.String [] Combined'保存在桌面上。显然,名称和用户选择的文件路径存在冲突。

我目前有输入文件夹选项,但输出+文件名似乎没有正常工作。

任何建议或帮助将不胜感激,谢谢。

2 个答案:

答案 0 :(得分:2)

string outputFileName = (sourceFileOutput + "Combined.docx");

这应该是

string outputFileName = selectedFolder + "Combined.docx";

尽管如此,请使用Path.Combine组合路径的两个部分。

答案 1 :(得分:0)

让程序使用'选择'目的地。

   // output destintion
    string outputFolder = @"";
    private void browseButtonOut_Click(object sender, EventArgs e)
    {
        FolderBrowserDialog diagBrowserOutput = new FolderBrowserDialog();
        diagBrowserOutput.Description = "Select a folder location to save the document...";

        // Default folder, altered when the user selects folder of choice 
        diagBrowserOutput.SelectedPath = outputFolder;

        // output file path display
        outputPath.Text = diagBrowserOutput.SelectedPath;

        if (DialogResult.OK == diagBrowserOutput.ShowDialog())
        {
            outputFolder = diagBrowserOutput.SelectedPath;
            outputPath.Text = diagBrowserOutput.SelectedPath;
        }
    }
    private void combineButton_Click(object sender, EventArgs e)
    {
        if (sourceFiles != null && sourceFiles.Length > 0)
        {
            string folderFolder = outputFolder;
            string outputFile = "Combined.docx";
            string outputFileName = Path.Combine(folderFolder, outputFile);
            MsWord.Merge(sourceFiles, outputFileName, true);

            // Message displaying how many files are combined. 
            MessageBox.Show("A total of " + sourceFiles.Length.ToString() + " documents have been merged", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

我按照建议使用了path.combine,因为我使用过的变量。