打开文件对话框的默认路径 [英] OpenFileDialog default path

查看:369
本文介绍了打开文件对话框的默认路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 使用(VAR openFileDialog1 =新的OpenFileDialog())
        {
            openFileDialog1.Reset();
            如果(!string.IsNullOrEmpty(ExcelFilePath))
            {
                字符串文件名= Path.GetFileName(ExcelFilePath);
                字符串fileExt = Path.GetExtension(ExcelFilePath);
                //避免使用该程序文件无法打开这个位置对话框
                //如果有在路径中的文件名剥离它)
                如果(!string.IsNullOrEmpty(文件名))
                    initialDirectory = Path.GetDirectoryName(ExcelFilePath);
      //如果不让它成为
                其他
                    initialDirectory = ExcelFilePath;            openFileDialog1.InitialDirectory = initialDirectory;
            }
            其他
                openFileDialog1.InitialDirectory =C:\\\\;
            openFileDialog1.Filter =Excel文件(* .xls或*的.xlsx)| * .xls的; *。XLSX
            //openFileDialog1.Filter =XLS文件(的* .xls)| * .xls的| XLSX文件|的.xlsx(* XLSX)。;
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = FALSE;
            openFileDialog1.CheckFileExists = TRUE;
            openFileDialog1.CheckPathExists = TRUE;
            如果(openFileDialog1.ShowDialog()== DialogResult.OK)
            {
                VAR browseSelectionMade = BrowseSelectionMade;
                如果(browseSelectionMade!= NULL)
                    browseSelectionMade(这一点,新DataEventArgs<串GT;(openFileDialog1.FileName));
            }
        }

不管我是否没有设置RestoreDirectory为真,我会一直浏览到最后使用的目录,如果我最初的目录设置为不存在的路径。哪里是最后使用的目录中保存的打开文件对话框?而且是有没有办法覆盖此行为? (例如,我总是想将其设置到C:\\如果初始目录不存在吗?)


解决方案

  

在哪里上次使用的目录保存?


据存储在注册表中。确切位置取决于Windows版本,Win7的是HKEY_CURRENT_USER \\软件\\微软\\的Windows \\ CurrentVersion \\ Explorer中\\ COMDLG32。就让我们来看看与注册表编辑器应该说服你,你的的想惹的。

最简单的解决方法是提供一个有效的路径。如果计算所述一个是无效的,Directory.Exists返回false,则提供一个有效的。像Environment.GetFolderPath返回的文档文件夹()。话又说回来,没有错,上次使用的一支,用户将很容易与良好的赔率承认它,它正好是接近理想的。

using (var openFileDialog1 = new OpenFileDialog())
        {
            openFileDialog1.Reset();
            if (!string.IsNullOrEmpty(ExcelFilePath))
            {
                string fileName = Path.GetFileName(ExcelFilePath);
                string fileExt = Path.GetExtension(ExcelFilePath);
                //Avoid "you can't open this location using this program file" dialog 
                //if there is a file name in the path strip it )
                if (!string.IsNullOrEmpty(fileName))
                    initialDirectory = Path.GetDirectoryName(ExcelFilePath);  
      //if not let it be   
                else
                    initialDirectory = ExcelFilePath;

            openFileDialog1.InitialDirectory = initialDirectory;
            }
            else
                openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.Filter = "Excel files (*.xls or *.xlsx)|*.xls;*.xlsx";
            //openFileDialog1.Filter = "xls files (*.xls)|*.xls|xlsx files(*.xlsx)|.xlsx";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = false;
            openFileDialog1.CheckFileExists = true;
            openFileDialog1.CheckPathExists = true;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                var browseSelectionMade = BrowseSelectionMade;
                if (browseSelectionMade!=null)
                    browseSelectionMade(this, new DataEventArgs<string>(openFileDialog1.FileName));
            }
        }

Regardless of whether or not I set RestoreDirectory to true, I will always browse to the LAST used directory if my initial directory is set to a path that doesn't exist. Where is the last used directory saved by OpenFileDialog? And is there a way to override this behavior? (e.g. I always want to set it to C:\ if the initial directory doesn't exist?)

解决方案

Where is the last used directory saved?

It is stored in the registry. The exact location depends on the Windows version, for Win7 it is HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\ComDlg32. A quick look with regedit ought to convince you that you don't want to mess with that.

The simple workaround is to provide a valid path. If the one you calculate isn't valid, Directory.Exists returns false, then provide a valid one. Like the Documents folder returned by Environment.GetFolderPath(). Then again, nothing wrong with the last used one either, the user will easily recognize it with good odds that it happens to be close to the desired one.

这篇关于打开文件对话框的默认路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆