帮助设置文件夹路径的大小
我正在尝试编写一个应用程序来使用以下代码计算一组股票的大小。然而问题是,随着搜索深入到共享,循环中的文件路径变量变得很大,抛出异常,因此无法继续。我发现一些内容说结合 @"\\?\"
可以增加字符数,但我不知道如何正确附加它。正如您所期望的,我的共享采用 \\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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以这样做:
You could just do something like this:
试试这个:
try this instead: