I have two buttons on a Windows Form, one called Browse
and the other Called Change Location
.
I would like to be able to set the InitialDirectory
value to be different for each of these buttons.
The reason for this is that the Browse
button opens images to display on screen and the Change Location
saves a text file to the selected location. I wish for these folders to be different.
I have provided code below for what I have so far:
//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);
}
}
Is there anything I am missing here?
I have set a different InitialDirectory
value in each method.
I have also used the string without the @symbol and changed the \'s to /'s but this still does not work.
It seems that no matter which button I click on, the pigeonImages
folder always displays.
I have also tried a variation of RestoreDirectory
in each method and in both methods and in neither method but this does not work either.
Thanks in advance.
When I send the openFileDialog1.InitialDirectory
to the console, I get this as output:
C:\path\to\my\program\here\bin\Debug\..\..\pigeonImages\
I always thought that typing ..\
would take you up a folder level.
I am used to writing code on a Mac, does Windows have a different way to go up a folder?
I believe your issue is beacuse of ....\
Do the following instead.
create a var so that you only need to call your applications current location once
//needed namespaces
using System.Reflections
using System.IO;
private string appDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
now in your buttons
openFileDialog1.InitialDirectory = Path.Combine(appDir, "dir1", "dir2");