1. Directory.Exists(); File.Exists();2. string[] drives=directory.getlogicdrives(); for(int i=0; i<drives.length; i++) { console.writeline(drives[i]); }3. public static long GetDirectoryLength(string dirPath) { //判断给定的路径是否存在,如果不存在则退出 if (!Directory.Exists(dirPath)) return 0; long len = 0; //定义一个DirectoryInfo对象 DirectoryInfo di = new DirectoryInfo(dirPath); //通过GetFiles方法,获取di目录中的所有文件的大小 foreach (FileInfo fi in di.GetFiles()) { len += fi.Length; } //获取di中所有的文件夹,并存到一个新的对象数组中,以进行递归 DirectoryInfo[] dis = di.GetDirectories(); if (dis.Length > 0) { for (int i = 0; i < dis.Length; i++) { len += GetDirectoryLength(dis[i].FullName); } } return len; }
Directory.Exists(); File.Exists(); float count=0; DirectoryInfo di=new DirectoryInfo(path); FileInfo[] SubFiles=di.GetFiles(); FileSystemInfo[] dirs = di.GetDirectories(); foreach(FileInfo fileNext in SubFiles) { count+=fileNext .Length } foreach(DirectoryInfo diNext in dirs) { } string[] fileList = Directory.GetFileSystemEntries( Path); for (int i = 0; i < fileList.Length; i++) { if (Directory.Exists(fileList[i])) { } else {
} }
foreach (DriveInfo di in DriveInfo.GetDrives()) { MessageBox.Show(di.VolumeLabel + "(" + di.Name + ")剩余空间:" + di.TotalFreeSpace.ToString()); }
public static void GetFile(string strFolderPath) { try { DirectoryInfo di = new DirectoryInfo(strFolderPath); FileSystemInfo[] fsinfo = di.GetFileSystemInfos(); foreach (FileSystemInfo fs in fsinfo) { if (fs is FileInfo) {
} if (fs is DirectoryInfo) { GetFile(fs.FullName); } } } catch (Exception ex) {
} }
写了个C#调系统命令的方法,楼主与递归调用比较一下,哪个效率还高?// 取得目录下的字节数 public int GetPathSize(string path) { if (!Directory.Exists(path)) return -1; Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.Arguments = "/c dir /-c/s " + path; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; p.Start(); string size; while (true) { if (p.StandardOutput.ReadLine().IndexOf("所列文件总数") > -1) { size = p.StandardOutput.ReadLine(); size = size.Substring(size.IndexOf("文件") + 2).Trim(); size = size.Substring(0, size.IndexOf("字节")).Trim(); break; } } return Convert.ToInt32(size); }//测试用代码 private void button4_Click(object sender, EventArgs e) { MessageBox.Show(GetPathSize("c:\\temp").ToString() + "字节"); }
14楼更正 int --> Int64 在测试 C:\\windows 时 int 超界了// 取得目录下的字节数 public Int64 GetPathSize(string path) { if (!Directory.Exists(path)) return -1; Process p = new Process(); p.StartInfo.FileName = "cmd.exe"; p.StartInfo.Arguments = "/c dir /-c/s " + path; p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardInput = true; p.StartInfo.RedirectStandardOutput = true; p.StartInfo.RedirectStandardError = true; p.StartInfo.CreateNoWindow = true; p.Start(); string size; while (true) { if (p.StandardOutput.ReadLine().IndexOf("所列文件总数") > -1) { size = p.StandardOutput.ReadLine(); size = size.Substring(size.IndexOf("文件") + 2).Trim(); size = size.Substring(0, size.IndexOf("字节")).Trim(); break; } } return Convert.ToInt64(size); }//测试用代码 private void button4_Click(object sender, EventArgs e) { MessageBox.Show(GetPathSize("c:\\temp").ToString() + "字节"); }
Directory.Exists("C:\\windows\\hh.exe") //返回true 是目录
string path="C:\\windows\\hh.exe";
File.Exists(path); // true 是文件
Directory.Exists(path); // true 是目录
// 都返回false 不存在
2:文件系统的根目录?有什么必要吗?DriveInfo.GetDrives 方法 (检索计算机上的所有逻辑驱动器的驱动器名称。 )
3:目录的大小就递归实现吧~WINDOWS本身也是用递归方法的……
Console.WriteLine(Directory.Exists(@"E:\虚拟机")); //true
2.不太理解你的意思,我的电脑不是一个实际目录,你想得到什么?
3.用FSO,.Net有专门的封装,好像在System.Scrpting.dll里,你查一下.net下的FSO
for(int i=0; i<drives.length; i++)
{
console.writeline(drives[i]);
}3. public static long GetDirectoryLength(string dirPath)
{
//判断给定的路径是否存在,如果不存在则退出
if (!Directory.Exists(dirPath))
return 0;
long len = 0; //定义一个DirectoryInfo对象
DirectoryInfo di = new DirectoryInfo(dirPath); //通过GetFiles方法,获取di目录中的所有文件的大小
foreach (FileInfo fi in di.GetFiles())
{
len += fi.Length;
} //获取di中所有的文件夹,并存到一个新的对象数组中,以进行递归
DirectoryInfo[] dis = di.GetDirectories();
if (dis.Length > 0)
{
for (int i = 0; i < dis.Length; i++)
{
len += GetDirectoryLength(dis[i].FullName);
}
}
return len;
}
的确没有好的方法
2. System.Environment.GetFolderPath(Environment.SpecialFolder.Programs);
楼主可以参考
3。 目录在文件系统是没有大小的 只能通过文件叠加 可以参考 《操作系统》
File.Exists(); float count=0;
DirectoryInfo di=new DirectoryInfo(path);
FileInfo[] SubFiles=di.GetFiles();
FileSystemInfo[] dirs = di.GetDirectories();
foreach(FileInfo fileNext in SubFiles)
{
count+=fileNext .Length
}
foreach(DirectoryInfo diNext in dirs)
{ }
string[] fileList = Directory.GetFileSystemEntries(
Path);
for (int i = 0; i < fileList.Length; i++)
{ if (Directory.Exists(fileList[i]))
{
}
else
{
}
}
foreach (DriveInfo di in DriveInfo.GetDrives())
{
MessageBox.Show(di.VolumeLabel + "(" + di.Name + ")剩余空间:" + di.TotalFreeSpace.ToString());
}
{
try
{
DirectoryInfo di = new DirectoryInfo(strFolderPath); FileSystemInfo[] fsinfo = di.GetFileSystemInfos();
foreach (FileSystemInfo fs in fsinfo)
{
if (fs is FileInfo)
{
}
if (fs is DirectoryInfo)
{
GetFile(fs.FullName);
}
}
}
catch (Exception ex)
{
}
}
public int GetPathSize(string path)
{
if (!Directory.Exists(path)) return -1;
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c dir /-c/s " + path;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start(); string size;
while (true)
{
if (p.StandardOutput.ReadLine().IndexOf("所列文件总数") > -1)
{
size = p.StandardOutput.ReadLine();
size = size.Substring(size.IndexOf("文件") + 2).Trim();
size = size.Substring(0, size.IndexOf("字节")).Trim();
break;
}
}
return Convert.ToInt32(size);
}//测试用代码
private void button4_Click(object sender, EventArgs e)
{
MessageBox.Show(GetPathSize("c:\\temp").ToString() + "字节");
}
public Int64 GetPathSize(string path)
{
if (!Directory.Exists(path)) return -1;
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
p.StartInfo.Arguments = "/c dir /-c/s " + path;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;
p.Start(); string size;
while (true)
{
if (p.StandardOutput.ReadLine().IndexOf("所列文件总数") > -1)
{
size = p.StandardOutput.ReadLine();
size = size.Substring(size.IndexOf("文件") + 2).Trim();
size = size.Substring(0, size.IndexOf("字节")).Trim();
break;
}
}
return Convert.ToInt64(size);
}//测试用代码
private void button4_Click(object sender, EventArgs e)
{
MessageBox.Show(GetPathSize("c:\\temp").ToString() + "字节");
}
Environment.GetFolderPath(Environment.SpecialFolder.MyComputer)