我想设计一个包含浏览按钮的程序,我们可以浏览到所选文件夹并打开文件夹内的文件.
我需要一个参考和阅读,我可以解决我的问题?喜欢我应该使用什么方法/类.我不喜欢从MSDN上读书,因为我很难理解他们的理论.仅供参考我仍然是C#的初学者.
非常感谢你
P/s:这是我从互联网上找到的代码,您可以浏览/创建新文件夹.但我不知道为什么它使用Shell32.dll ..
private void button1_Click(object sender, EventArgs e)
{
string strPath;
string strCaption = "Select a Directory and folder.";
DialogResult dlgResult;
Shell32.ShellClass shl = new Shell32.ShellClass();
Shell32.Folder2 fld = (Shell32.Folder2)shl.BrowseForFolder(0, strCaption, 0,
System.Reflection.Missing.Value);
if (fld == null)
{
dlgResult = DialogResult.Cancel;
}
else
{
strPath = fld.Self.Path;
dlgResult = DialogResult.OK;
}
}
Run Code Online (Sandbox Code Playgroud)
Sve*_*lov 10
来自msdn
private void button1_Click(object sender, System.EventArgs e)
{
Stream myStream = null;
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)
{
try
{
if ((myStream = openFileDialog1.OpenFile()) != null)
{
using (myStream)
{
// Insert code to read the stream here.
}
}
}
catch (Exception ex)
{
MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
}
}
}
Run Code Online (Sandbox Code Playgroud)