restore last folder in openFileDialog in c# - Tutorial on creating desktop apps in C# language

preview_player
Показать описание
To access the full course in order, click on the link below:-

Keep the last selected path using OpenFileDialog / C ...
c# - Get last used directory
How do I keep track of the last folder selected by a user?
How to retrieve the last folder used by OpenFileDialog?
how to open folder and select file in c#
powershell openfiledialog select folder
c# browse for file path
vb net openfiledialog default path
select file from folder in c#
vb net openfiledialog select folder
get directory in c#
c# get last directory in path

FileDialog.RestoreDirectory Property

Definition

Namespace:
System.Windows.Forms

Assembly:

Gets or sets a value indicating whether the dialog box restores the directory to the previously selected directory before closing.
C#

public bool RestoreDirectory { get; set; }

Property Value

Boolean

true if the dialog box restores the current directory to the previously selected directory if the user changed the directory while searching for files; otherwise, false. The default value is false.
Examples

The following code example uses the OpenFileDialog implementation of FileDialog and illustrates creating, setting of properties, and showing the dialog box. The example uses the RestoreDirectory property to ensure that the previously selected directory is restored when the dialog box is closed. The example requires a form with a Button placed on it and the System.IO namespace added to it.
C#

var fileContent = string.Empty;
var filePath = string.Empty;

using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.InitialDirectory = "c:\\";
openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog.FilterIndex = 2;
openFileDialog.RestoreDirectory = true;

if (openFileDialog.ShowDialog() == DialogResult.OK)
{
//Get the path of specified file
filePath = openFileDialog.FileName;

//Read the contents of the file into a stream
var fileStream = openFileDialog.OpenFile();

using (StreamReader reader = new StreamReader(fileStream))
{
fileContent = reader.ReadToEnd();
}
}
}

MessageBox.Show(fileContent, "File Content at path: " + filePath, MessageBoxButtons.OK);

source
Рекомендации по теме
welcome to shbcf.ru