帮助设置文件夹路径的大小

发布于 2024-12-05 17:06:06 字数 1606 浏览 0 评论 0原文

我正在尝试编写一个应用程序来使用以下代码计算一组股票的大小。然而问题是,随着搜索深入到共享,循环中的文件路径变量变得很大,抛出异常,因此无法继续。我发现一些内容说结合 @"\\?\" 可以增加字符数,但我不知道如何正确附加它。正如您所期望的,我的共享采用 \\server\name 的形式。

谢谢。

try
{
    //Checks if the path is valid or not
    if (!Directory.Exists(folder))
    {
        return folderSize;
    }
    else
    {
        try
        {
            foreach (string filePath in Directory.GetFiles(folder))
            {
                if (File.Exists(filePath))
                {
                    FileInfo finfo = new FileInfo(filePath);
                    folderSize += finfo.Length;
                }
            }

            foreach (string dir in Directory.GetDirectories(folder))
                folderSize += GetDirectorySize(dir);
        }
        catch (NotSupportedException e)
        {
            Console.WriteLine("Unable to calculate folder size: {0}", e.Message);
        }
    }
}

尝试建议答案后抛出异常

    'A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
'ShareSizes.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Could not find file 'Shortcut to fileName'.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileInfo.get_Length()
   at ShareSizes.Form1.button1_Click(Object sender, EventArgs e) in C:\Documents and Settings\...line 50

i am trying to write an application to calculate the size of a set of shares using the following code. The issue however is that as the search gets deep in to the shares, the file path variable in the loop becomes to large an exception gets thrown and therefore cant continue. I found something which says that combining @"\\?\" allows the charachter count to increase but i cant figure out how to append it properly. My share takes the form of \\server\name as you would expect.

Thanks.

try
{
    //Checks if the path is valid or not
    if (!Directory.Exists(folder))
    {
        return folderSize;
    }
    else
    {
        try
        {
            foreach (string filePath in Directory.GetFiles(folder))
            {
                if (File.Exists(filePath))
                {
                    FileInfo finfo = new FileInfo(filePath);
                    folderSize += finfo.Length;
                }
            }

            foreach (string dir in Directory.GetDirectories(folder))
                folderSize += GetDirectorySize(dir);
        }
        catch (NotSupportedException e)
        {
            Console.WriteLine("Unable to calculate folder size: {0}", e.Message);
        }
    }
}

exception thrown after trying suggested answers

    'A first chance exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll
'ShareSizes.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
Could not find file 'Shortcut to fileName'.
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileInfo.get_Length()
   at ShareSizes.Form1.button1_Click(Object sender, EventArgs e) in C:\Documents and Settings\...line 50

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦言归人 2024-12-12 17:06:06

你可以这样做:

DirectoryInfo di = new DirectoryInfo(rootFolder);
foreach (FileInfo finfo in di.GetFiles("*.*", SearchOption.AllDirectories) 
{
   folderSize += finfo.Length;  
}

You could just do something like this:

DirectoryInfo di = new DirectoryInfo(rootFolder);
foreach (FileInfo finfo in di.GetFiles("*.*", SearchOption.AllDirectories) 
{
   folderSize += finfo.Length;  
}
恬淡成诗 2024-12-12 17:06:06

试试这个:

string path = @"\\Server\Share";

System.IO.DirectoryInfo dInfo = new System.IO.DirectoryInfo(path);
long totalSize = 0;

foreach (FileInfo fInfo in dInfo.GetFiles("*", SearchOption.AllDirectories)) {
    totalSize += fInfo.Length;

}

Console.Out.WriteLine(totalSize.ToString());

try this instead:

string path = @"\\Server\Share";

System.IO.DirectoryInfo dInfo = new System.IO.DirectoryInfo(path);
long totalSize = 0;

foreach (FileInfo fInfo in dInfo.GetFiles("*", SearchOption.AllDirectories)) {
    totalSize += fInfo.Length;

}

Console.Out.WriteLine(totalSize.ToString());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文