C# OpenFileDialog和SaveFileDialog是否可以具有不同的InitialDirectory值
我在Windows窗体上有两个按钮,一个叫做C# OpenFileDialog和SaveFileDialog是否可以具有不同的InitialDirectory值,c#,openfiledialog,savefiledialog,C#,Openfiledialog,Savefiledialog,我在Windows窗体上有两个按钮,一个叫做浏览,另一个叫做更改位置 我希望能够为每个按钮设置不同的InitialDirectory值 原因是浏览按钮打开图像以显示在屏幕上,而更改位置将文本文件保存到所选位置。我希望这些文件夹是不同的 我已经提供了以下代码,用于我目前所拥有的: //Code for the browse button private void browseButton_Click(object sender, EventArgs e) { using (OpenFile
浏览
,另一个叫做更改位置
我希望能够为每个按钮设置不同的InitialDirectory
值
原因是浏览
按钮打开图像以显示在屏幕上,而更改位置
将文本文件保存到所选位置。我希望这些文件夹是不同的
我已经提供了以下代码,用于我目前所拥有的:
//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
值。
我还使用了不带@符号的字符串,并将‘’改为‘’,但这仍然不起作用
似乎无论我点击哪个按钮,都会显示pigeimages
文件夹。
我还尝试了在每种方法和两种方法中以及两种方法都使用restoreditory
的变体,但这两种方法都不起作用
提前谢谢
当我将
openFileDialog1.InitialDirectory
发送到控制台时,我得到以下输出:
C:\path\to\my\program\here\bin\Debug\..\..\..\pigeimages\
我一直认为键入。\
会让你进入一个文件夹级别。
我习惯于在Mac电脑上编写代码,Windows是否有不同的方式进入文件夹?我相信您的问题是因为 改为执行以下操作 创建一个var,这样您只需要调用应用程序当前位置一次
//needed namespaces
using System.Reflections
using System.IO;
private string appDir = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
现在按你的钮扣
openFileDialog1.InitialDirectory = Path.Combine(appDir, "dir1", "dir2");
您是否已尝试将这两个文件夹的.InitialDirectory设置为完整路径,而不是相对路径。@WhoIsRich我会,但我计划在许多计算机上使用此程序。文件夹将始终与此程序在一起,但不一定在每个计算机硬盘驱动器上的相同位置,然后使用var folder=Path.GetDirectoryName(Assembly.getExecutionGassembly().Location);获取程序正在运行的位置并附加到它。在
ShowDialog
之前放置一个断点,并添加一个监视表达式目录。Exists(@.\..\digeimages”)
。还要检查对话框是否设置了初始路径……为了确保,我的按钮应该是:openFileDialog1.InitialDirectory=path.Combine(appDir,@.\..\digeimages\”代码>您不想使用..\。。在您的代码中,appDir将为您获取应用程序的位置,然后在图像中使用目录名。您是一个传奇人物。这两个按钮现在都可以打开单独的文件夹,以及它们应该打开的文件夹。另外+1用于包含System.Reflection
我以前无法获取@WhoIsRich的工作路径。