在线等 大家帮一下忙

luoaizhou 2008-07-07 11:42:14

上面是我的数据库结构,我想求得指定文件夹下(包括子文件夹)所有文件的大小。ASP。NET(C#)
求算法
DirID 文件夹或文件编号 ParentID 父目录ID Contain 文件大小 Flag 是否为文件夹

在线等。。。 急
...全文
119 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
wudi626 2008-07-09
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 luoaizhou 的回复:]
如果文件夹和文件都存在的话 好实现 , 现在的问题是 在机器上就一个文件夹, 这个文件夹下的所有文件夹都是虚有的,只是在数据库中做了文章。 这算法小弟写不出来了。。
我运行结果


我的图片和我的文档在实际中是不存在这两个文件夹的,只是在数据库的有这个记录。这样的话计算起来就不好计算了,我知道可以用递归实现,但是算法怎么写 不会了。
[/Quote]


这个应该是虚拟文件服务系统的方法来实现!
zero8500 2008-07-07
  • 打赏
  • 举报
回复
帮顶
luoaizhou 2008-07-07
  • 打赏
  • 举报
回复
如果文件夹和文件都存在的话 好实现 , 现在的问题是 在机器上就一个文件夹, 这个文件夹下的所有文件夹都是虚有的,只是在数据库中做了文章。 这算法小弟写不出来了。。
我运行结果


我的图片和我的文档在实际中是不存在这两个文件夹的,只是在数据库的有这个记录。这样的话计算起来就不好计算了,我知道可以用递归实现,但是算法怎么写 不会了。
wudi626 2008-07-07
  • 打赏
  • 举报
回复
记得碰到过FileOrFoder对象,利用这个对象可以实现的

void FindFile( Directory d )
{
FileOrFolders = d.GetFileOrFolders();
foreach( FileOrFolder fof in FileOrFolders )
{
if( fof is File )
You Found a file;
else if ( fof is Directory )
FindFile( fof );
}
}

利用这段功能相似的代码,相信可以解决你的问题!
wudi626 2008-07-07
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 luoaizhou 的回复:]
这个我想到了,问题是 一个文件夹下有两个文件夹 我如果执行第一个文件夹下那什么时候才执行第二个文件夹中的呀?
[/Quote]

可以递归调用实现的哈!
luoaizhou 2008-07-07
  • 打赏
  • 举报
回复
自己 在顶一下。。。
luoaizhou 2008-07-07
  • 打赏
  • 举报
回复
急呀 , 希望有人可以写出代码
luoaizhou 2008-07-07
  • 打赏
  • 举报
回复
这个我想到了,问题是 一个文件夹下有两个文件夹 我如果执行第一个文件夹下那什么时候才执行第二个文件夹中的呀?
bestlove2008 2008-07-07
  • 打赏
  • 举报
回复
1.首先获取指定文件夹的 dirid好吗;
2.然后用递归的方法检索每条记录;如果该记录的父目录为dirid且另行为文件则指定目录的容量增加该文件的大小
3.如果为文件夹则循环执行2
shuihao2004 2008-07-07
  • 打赏
  • 举报
回复
顶!!!
luoaizhou 2008-07-07
  • 打赏
  • 举报
回复
谢谢 可能你们没明白我的意思, 我现在做的是 每一个用户就一个文件夹,这个用户 所谓的我的图片 和我的文档,在实际中不是存在的,每一个用户上传的所有文件都存储在以用户名为文件夹名的这一个文件夹下,在显示时,文件夹和文件信息都在数据库中的,用Windows 自带的类 没办法用。 只能自己写方法,我知道可以用递归,但是不知道怎么写。

上面是我的数据库结构。
xlong224 2008-07-07
  • 打赏
  • 举报
回复
递归 遍历
一品梅 2008-07-07
  • 打赏
  • 举报
回复
嗯,还是改成FileInfo吧,因为File没有属性,用FileInfo直接用length求出文件大小。
一品梅 2008-07-07
  • 打赏
  • 举报
回复
不好意思,帖错。
// For Directory.GetFiles and Directory.GetDirectories
// For File.Exists, Directory.Exists
using System;
using System.IO;
using System.Collections;

public class RecursiveFileProcessor
{
public static void Main(string[] args)
{
foreach(string path in args)
{
if(File.Exists(path))
{
// This path is a file
ProcessFile(path);
}
else if(Directory.Exists(path))
{
// This path is a directory
ProcessDirectory(path);
}
else
{
Console.WriteLine("{0} is not a valid file or directory.", path);
}
}
}


// Process all files in the directory passed in, recurse on any directories
// that are found, and process the files they contain.
public static void ProcessDirectory(string targetDirectory)
{
// Process the list of files found in the directory.
string [] fileEntries = Directory.GetFiles(targetDirectory);
foreach(string fileName in fileEntries)
ProcessFile(fileName);

// Recurse into subdirectories of this directory.
string [] subdirectoryEntries = Directory.GetDirectories(targetDirectory);
foreach(string subdirectory in subdirectoryEntries)
ProcessDirectory(subdirectory);
}

// Insert logic for processing found files here.
public static void ProcessFile(string path)
{
Console.WriteLine("Processed file '{0}'.", path);
}
}
一品梅 2008-07-07
  • 打赏
  • 举报
回复
using System;
using System.IO;

class Test
{
public static void Main()
{
// Specify the directories you want to manipulate.
string path = @"c:\MyDir";
string target = @"c:\TestDir";

try
{
// Determine whether the directory exists.
if (!Directory.Exists(path))
{
// Create the directory it does not exist.
Directory.CreateDirectory(path);
}

if (Directory.Exists(target))
{
// Delete the target to ensure it is not there.
Directory.Delete(target, true);
}

// Move the directory.
Directory.Move(path, target);

// Create a file in the directory.
File.CreateText(target + @"\myfile.txt");

// Count the files in the target directory.
Console.WriteLine("The number of files in {0} is {1}",
target, Directory.GetFiles(target).Length);
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
finally {}
}
}
一品梅 2008-07-07
  • 打赏
  • 举报
回复
没有必要用数据库来做,你用文件系统的FILE和Directory类就可以实现。
banditi225 2008-07-07
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 zero8500 的回复:]
帮顶
[/Quote]

61,817

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧