您的位置:首页 > 运维架构

FolderBrowserDialog 和 OpenFileDialog

2012-05-12 09:52 676 查看

FolderBrowserDialog :

1.提示用户选择文件夹;
2.此类提供一种方法,它提示用户浏览、创建并最终选择一个文件夹。如果只允许用户选择文件夹而非文件,则可使用此类。文件夹的浏览通过树控件完成。只能选择文件系统中的文件夹;不能选择虚拟文件夹。
3.void Button1Click(object sender, EventArgs e)
{
FolderBrowserDialog fbd=new FolderBrowserDialog ();
fbd.Description="选择文件夹";
fbd.RootFolder=Environment.SpecialFolder.MyComputer;
fbd.ShowNewFolderButton=true;
if(fbd.ShowDialog()!=DialogResult.OK)
return;
string path=fbd.SelectedPath;
textBox1.Text=path;
}


通常在创建新 FolderBrowserDialog 后,将 RootFolder 设置为开始浏览的位置。或者,可将 SelectedPath 设置为最初选定的 RootFolder 子文件夹的绝对路径。也可以选择设置 Description 属性为用户提供附加说明。最后,调用 ShowDialog 方法将对话框显示给用户。如果该对话框关闭并且 ShowDialog 显示的对话框为 DialogResult..::.OK,SelectedPath 则是一个包含选定文件夹路径的字符串。如果用户可通过“新建文件夹”按钮创建新文件夹,则可对控件使用 ShowNewFolderButton 属性。


OpenFileDialog :

1.提示用户打开文件,使用此类可检查某个文件是否存在并打开该文件;
2.void Button1Click(object sender, EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();//类的实例化
openFileDialog1.InitialDirectory =Environment.GetFolderPath(Environment.SpecialFolder.MyComputer);//打开位置
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; //文件类型
openFileDialog1.FilterIndex = 1;//表示默认筛选情况
openFileDialog1.RestoreDirectory = true;//获取或设置一个值,该值指示对话框在关闭前是否还原当前目录。
openFileDialog1.ValidateNames = true;     //文件有效性验证ValidateNames,验证用户输入是否是一个有效的Windows文件名
openFileDialog1.CheckFileExists = true;  //验证路径有效性
openFileDialog1.CheckPathExists = true; //验证文件有效性
if (openFileDialog1.ShowDialog() != DialogResult.OK)
return;
string path=openFileDialog1.SafeFileName;
this.textBox1.AppendText(path);
}


.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: