|
| 1 | +--- |
| 2 | +title: Save and Restore the Last Opened Directory in RadFileDialogs |
| 3 | +description: How to implement save and restore last opened directory in RadOpenFileDialog. |
| 4 | +type: how-to |
| 5 | +page_title: Persist the Last Directory on the File System When Using RadFileDialog |
| 6 | +slug: kb-file-dialogs-save-load-last-opened-directory |
| 7 | +tags: radopenfiledialog, wpf, restoredirectory, mvvm, owner, dialog |
| 8 | +res_type: kb |
| 9 | +ticketid: 1632230 |
| 10 | +--- |
| 11 | + |
| 12 | +## Environment |
| 13 | + |
| 14 | +| Product | Version | |
| 15 | +| --- | --- | |
| 16 | +| RadFileDialogs for WPF | 2023.1.315 | |
| 17 | + |
| 18 | +## Description |
| 19 | + |
| 20 | +How to implement custom behavior that saves and restores the last opened directory when you show a RadFileDialog. |
| 21 | + |
| 22 | +## Solution |
| 23 | + |
| 24 | +To ensure `RadOpenFileDialog` remembers the last opened directory even across different application sessions, implement a custom logic to store the last directory path. This approach involves saving the directory path to a persistent storage, such as application settings or a file, and then retrieving it the next time the dialog is opened. |
| 25 | + |
| 26 | +#### __[C#]__ |
| 27 | +{{region kb-file-dialogs-save-load-last-opened-directory-0}} |
| 28 | + public partial class App : Application |
| 29 | + { |
| 30 | + private IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null); |
| 31 | + private const string dirCacheIsoStoreFileName = "RadOpenFileDialogDirectoryCache.txt"; |
| 32 | + private string fileDialogDirectoryCache = "C:\\Program Files (x86)"; |
| 33 | + |
| 34 | + protected override void OnStartup(StartupEventArgs e) |
| 35 | + { |
| 36 | + LoadDirectoryCacheFromStorage(); |
| 37 | + base.OnStartup(e); |
| 38 | + } |
| 39 | + |
| 40 | + protected override void OnExit(ExitEventArgs e) |
| 41 | + { |
| 42 | + SaveDirectoryCacheToStorage(); |
| 43 | + base.OnExit(e); |
| 44 | + } |
| 45 | + |
| 46 | + internal string GetDileDialogDirectoryCache() |
| 47 | + { |
| 48 | + return this.fileDialogDirectoryCache; |
| 49 | + } |
| 50 | + |
| 51 | + internal void UpdateFileDialogDirectoryCache(string newDirectory) |
| 52 | + { |
| 53 | + this.fileDialogDirectoryCache = newDirectory; |
| 54 | + } |
| 55 | + |
| 56 | + private void LoadDirectoryCacheFromStorage() |
| 57 | + { |
| 58 | + if (isoStore.FileExists(dirCacheIsoStoreFileName)) |
| 59 | + { |
| 60 | + using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(dirCacheIsoStoreFileName, FileMode.Open, isoStore)) |
| 61 | + { |
| 62 | + using (StreamReader reader = new StreamReader(isoStream)) |
| 63 | + { |
| 64 | + this.fileDialogDirectoryCache = reader.ReadToEnd(); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private void SaveDirectoryCacheToStorage() |
| 71 | + { |
| 72 | + using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(dirCacheIsoStoreFileName, FileMode.OpenOrCreate, isoStore)) |
| 73 | + { |
| 74 | + using (StreamWriter writer = new StreamWriter(isoStream)) |
| 75 | + { |
| 76 | + writer.Write(this.fileDialogDirectoryCache); |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | +{{endregion}} |
| 82 | + |
| 83 | +#### __[C#]__ |
| 84 | +{{region kb-file-dialogs-save-load-last-opened-directory-1}} |
| 85 | + private void OnShowDialogButton_Click(object sender, RoutedEventArgs e) |
| 86 | + { |
| 87 | + RadOpenFileDialog openFileDialog = new RadOpenFileDialog |
| 88 | + { |
| 89 | + InitialDirectory = ((App)App.Current).GetDileDialogDirectoryCache() |
| 90 | + }; |
| 91 | + |
| 92 | + if (openFileDialog.ShowDialog() == true) |
| 93 | + { |
| 94 | + string fileName = openFileDialog.FileName; |
| 95 | + ((App)App.Current).UpdateFileDialogDirectoryCache(System.IO.Path.GetDirectoryName(fileName)); |
| 96 | + } |
| 97 | + } |
| 98 | +{{endregion}} |
| 99 | + |
| 100 | + |
0 commit comments