OpenFileDialog和SaveFileDialog可以具有不同的InitialDirectory值

时间:2014-01-30 00:41:28

标签: c# openfiledialog savefiledialog

我在Windows窗体上有两个按钮,一个名为Browse,另一个名为Change Location

我希望能够为每个按钮设置InitialDirectory值不同。

原因是Browse按钮打开要在屏幕上显示的图像,Change Location将文本文件保存到所选位置。我希望这些文件夹不同。

我已经为我目前提供的代码提供了以下代码:

//Code for the browse button
private void browseButton_Click(object sender, EventArgs e)
{
    using (OpenFileDialog openFileDialog1 = new OpenFileDialog())
    {
        openFileDialog1.Title = "Select Image Files To Be Displayed";
        openFileDialog1.InitialDirectory = @"..\..\pigeonImages\";
        // Set filter options and filter index.
        openFileDialog1.Filter = "Image Files|*.png;*.jpeg;*.gif|All Files|*.*";
        openFileDialog1.FilterIndex = 2;
        openFileDialog1.FileName = String.Empty;
        //Allow for multiple files to be selected
        openFileDialog1.Multiselect = true;

        // Call the ShowDialog method to show the dialog box.
        DialogResult result = openFileDialog1.ShowDialog();

        if (result == DialogResult.OK)
        {
            //Create a stream which points to the open file
            Stream openFileStream = openFileDialog1.OpenFile();
            //Create a reader
            StreamReader reader = new StreamReader(openFileStream);
            //Read the contents of the stream
            string shortenedFilenames = String.Empty;
            StringBuilder totalFiles = new StringBuilder();
            //to get just one filename use openDialog1.FileName
            //to get multiple filenames use openDialog1.FileNames
            foreach(string file in openFileDialog1.FileNames)
            {
                shortenedFilenames = file.Substring(openFileDialog1.FileName.LastIndexOf(@"\") + 1);
                totalFiles.Append(shortenedFilenames).Append(", ");
            }
            string totalFilesResult = totalFiles.ToString();
            totalFilesResult = totalFilesResult.TrimEnd(' ');
            totalFilesResult = totalFilesResult.TrimEnd(',');
            tbResults.Text = totalFilesResult;
            System.Diagnostics.Debug.WriteLine("fileStream -> " + totalFilesResult);

            //Close the reader
            reader.Close();
        }
    }
}


//Code for the Change Location button
private void saveLocationButton_Click(object sender, EventArgs e)
{
    using (SaveFileDialog saveTextDialog = new SaveFileDialog())
    {
        saveTextDialog.Title = "Save File In Correct Folder!";
        saveTextDialog.InitialDirectory = @"..\..\testScores\";

        tbSaveLocation.BackColor = Color.White;
        tbSaveLocation.ForeColor = Color.Black;

        saveTextDialog.Filter = "Text Files | *.txt";
        saveTextDialog.DefaultExt = "txt";

        saveResult = saveTextDialog.ShowDialog();


        if (saveResult == DialogResult.OK)
        {
            Stream saveFileStream = saveTextDialog.OpenFile();
            using (StreamWriter writer = new StreamWriter(saveFileStream))
            {
                writer.Write("");
                writer.Flush();
                //writer.Close();
            }
        }
        tbSaveLocation.Text = String.Format("testScores/{0}/{0}.txt", birdIdString);
    }
}

这里有什么我想念的吗?

我在每个方法中设置了不同的InitialDirectory值。 我也使用了没有@symbol的字符串并将\更改为/'但是这仍然不起作用。

似乎无论我点击哪个按钮,都会显示pigeonImages文件夹。 我也在每种方法和两种方法中尝试了RestoreDirectory的变体,但两种方法都没有。但是这也不起作用。

提前致谢。


当我将openFileDialog1.InitialDirectory发送到控制台时,我将其作为输出:

C:\path\to\my\program\here\bin\Debug\..\..\pigeonImages\

我一直认为输入..\会占用文件夹级别。 我习惯在Mac上编写代码,Windows有不同的方法来上传文件夹吗?

1 个答案:

答案 0 :(得分:1)

我相信你的问题是因为...... \

请执行以下操作。

创建一个var,这样您只需要调用一次应用程序当前位置

//needed namespaces
using System.Reflections
using System.IO;

private string appDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

现在在你的按钮

openFileDialog1.InitialDirectory = Path.Combine(appDir, "dir1", "dir2");