首先要說明一下:
添加引用: System.Design
此文件在下面的位置
C:\Program Files\Reference Assemblies\Microsoft\Framework\.NETFramework\v4.0\System.Design.dll
有些人說在添加引用的面板中並找不到這個引用.這是由於你的目標框架設置錯誤
在 解決方案管理器上 右鍵 "屬性" --> 發布 --> 目標框架 修改為 .Net Framework 4.0 (沒有那個client )
這樣你再在添加引用面板中就可以找到這個引用了.
添加這兩個引用
using System.Windows.Forms;
using System.Windows.Forms.Design;
==================新建一個FolderDialog類 ==============
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Windows.Forms.Design;
public class FolderDialog : FolderNameEditor
{
FolderNameEditor.FolderBrowser fDialog = new FolderNameEditor.FolderBrowser();
public FolderDialog()
{
}
public DialogResult DisplayDialog()
{
return DisplayDialog("請選擇一個文件夾");
}
public DialogResult DisplayDialog(string description)
{
fDialog.Description = description;
return fDialog.ShowDialog();
}
public string Path
{
get { return fDialog.DirectoryPath; }
}
~FolderDialog() { fDialog.Dispose(); }
}
--------------調用--------------------
FolderDialog openFolder = new FolderDialog();
if (openFolder.DisplayDialog() == DialogResult.OK)
{
textBox1.Text = openFolder.Path.ToString();
}
else
{
textBox1.Text = "你沒有選擇目錄";
}
DirectoryInfo dirInfo = new DirectoryInfo(textBox1.Text);
FileInfo[] files = dirInfo.GetFiles();
foreach (FileInfo filename in files)
{
listBox1.Items.Add(filename);
}
下面的例子是一個獲取文件夾內的所有文件的
using System; using System.IO; namespace ConsoleApplication7 { /// <summary> /// Class1 的摘要說明 /// </summary> class Class1 { /// <summary> /// 應用程序的主入口點 /// </summary> [STAThread] static void Main(string[] args) { string dirp=@"d:\\d"; DirectoryInfo mydir = new DirectoryInfo(dirp); foreach (FileSystemInfo fsi in mydir.GetFileSystemInfos()) { if (fsi is FileInfo) { FileInfo fi = (FileInfo)fsi; string x=System.IO.Path.GetDirectoryName(fi.FullName); Console.WriteLine(x); string s=System.IO.Path.GetExtension(fi.FullName); string y=System.IO.Path.GetFileNameWithoutExtension(fi.FullName); Console.WriteLine(y); if(s==".jpg") { System.IO.File.Copy(fi.FullName,x+@"\oo"+fi.Name); //在原文件名前加上OO System.IO.File.Delete(fi.FullName); } } } Console.WriteLine("成功"); Console.ReadLine(); } }
