FileDialog.InitialDirectory 属性
获取或设置文件对话框显示的初始目录。
**命名空间:**System.Windows.Forms
**程序集:**System.Windows.Forms(在 system.windows.forms.dll 中)
语法
声明
Public Property InitialDirectory As String
用法
Dim instance As FileDialog
Dim value As String
value = instance.InitialDirectory
instance.InitialDirectory = value
public string InitialDirectory { get; set; }
public:
property String^ InitialDirectory {
String^ get ();
void set (String^ value);
}
/** @property */
public String get_InitialDirectory ()
/** @property */
public void set_InitialDirectory (String value)
public function get InitialDirectory () : String
public function set InitialDirectory (value : String)
属性值
文件对话框中显示的初始目录。默认值为空字符串 ("")。
备注
通常使用下列源之一设置 InitialDirectory 属性:
以前在程序中使用的路径,可能是从上次目录或文件操作保留下来的。
从持久性源读取的路径,如应用程序设置、Registry 或应用程序中的字符串资源。
标准 Windows 系统和用户路径,如 Program Files、MyDocuments、MyMusic 等。
与当前应用程序相关的路径,如其启动目录。
有关创建动态路径的更多信息,请参见 FileDialog 类概述。
示例
下面的代码示例使用 FileDialog 的 OpenFileDialog 实现,并演示如何创建和设置属性以及如何显示对话框。该示例使用 InitialDirectory 属性设置对话框对用户显示时的初始目录。该示例要求窗体上放置了一个 Button,并在其中添加了 System.IO 命名空间。
Private Sub button1_Click(sender As Object, e As System.EventArgs)
Dim myStream As Stream
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory = "c:\"
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog() = DialogResult.OK Then
myStream = openFileDialog1.OpenFile()
If Not (myStream Is Nothing) Then
' Insert code to read the stream here.
myStream.Close()
End If
End If
End Sub
private void button1_Click(object sender, System.EventArgs e)
{
Stream myStream;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "c:\\" ;
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
openFileDialog1.FilterIndex = 2 ;
openFileDialog1.RestoreDirectory = true ;
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
if((myStream = openFileDialog1.OpenFile())!= null)
{
// Insert code to read the stream here.
myStream.Close();
}
}
}
private:
void button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
{
Stream^ myStream;
OpenFileDialog^ openFileDialog1 = gcnew OpenFileDialog;
openFileDialog1->InitialDirectory = "c:\\";
openFileDialog1->Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
openFileDialog1->FilterIndex = 2;
openFileDialog1->RestoreDirectory = true;
if ( openFileDialog1->ShowDialog() == ::DialogResult::OK )
{
if ( (myStream = openFileDialog1->OpenFile()) != nullptr )
{
// Insert code to read the stream here.
myStream->Close();
}
}
}
protected void button1_Click(Object sender, System.EventArgs e)
{
Stream myStream;
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.set_InitialDirectory("c:\\");
openFileDialog1.set_Filter(
"txt files (*.txt)|*.txt|All files (*.*)|*.*");
openFileDialog1.set_FilterIndex(2);
openFileDialog1.set_RestoreDirectory(true);
if (openFileDialog1.ShowDialog().Equals(get_DialogResult().OK)) {
if ((myStream = openFileDialog1.OpenFile()) != null) {
// Insert code to read the stream here.
myStream.Close();
}
}
} //button1_Click
.NET Framework 安全性
- FileIOPermission 要求设置初始目录。关联的枚举:PermissionState.Unrestricted。
平台
Windows 98、Windows 2000 SP4、Windows CE、Windows Millennium Edition、Windows Mobile for Pocket PC、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition
.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求。
版本信息
.NET Framework
受以下版本支持:2.0、1.1、1.0
.NET Compact Framework
受以下版本支持:2.0、1.0
请参见
参考
FileDialog 类
FileDialog 成员
System.Windows.Forms 命名空间
RestoreDirectory
Registry
Application 类
System.Environment
System.IO.Path