模拟获取份额大小的过程
我编写了一些代码来模拟获取共享驱动器大小的过程。驱动器来自文本文件。问题是路径达到了超过 250 的阶段?字符限制。无论如何,这是否可以避免。
我在网上发现了一些建议在文件路径之前放置 @"\?\" 的内容,但不确定它是否能正常工作以及我是否正确使用它?
谢谢
void getSizes_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
String val = "";
float megabytes = ((float)e.Result / 1024f) / 1024f;
if (megabytes > 10240) //greater than 10 gig
val = (megabytes / 1024.0).ToString() + "GB";
else
val = megabytes + "MB";
textBox1.Text += val;
textBox1.Text += Environment.NewLine;
}
void getSizes_DoWork(object sender, DoWorkEventArgs e)
{
String s = (String)e.Argument;
String path = Path.GetFullPath(s);
float bytes = (float)GetDirectorySize((String)e.Argument);
e.Result = bytes;
}
protected static float GetDirectorySize(string folder)
{
float folderSize = 0.0f;
try
{
//Checks if the path is valid or not
if (!Directory.Exists(folder))
return folderSize;
else
{
try
{
foreach (string file in Directory.GetFiles(folder))
{
String path = @"\\?\" + file;
if (File.Exists(path))
{
FileInfo finfo = new FileInfo(path);
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);
}
}
}
catch (UnauthorizedAccessException e)
{
Console.WriteLine("Unable to calculate folder size: {0}", e.Message);
}
return folderSize;
}
}
}
I have written some code to simulate the process of gaining share drive sizes. The drives come from a text file. The problem is that the paths get to a stage which exceed the 250? charachter limit. Is there anyway this can be avoided.
I found something on-line which suggested placing @"\?\" before the filepath but am unsurue if it will work for definate and if i am using it correctly or not?
Thanks
void getSizes_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
String val = "";
float megabytes = ((float)e.Result / 1024f) / 1024f;
if (megabytes > 10240) //greater than 10 gig
val = (megabytes / 1024.0).ToString() + "GB";
else
val = megabytes + "MB";
textBox1.Text += val;
textBox1.Text += Environment.NewLine;
}
void getSizes_DoWork(object sender, DoWorkEventArgs e)
{
String s = (String)e.Argument;
String path = Path.GetFullPath(s);
float bytes = (float)GetDirectorySize((String)e.Argument);
e.Result = bytes;
}
protected static float GetDirectorySize(string folder)
{
float folderSize = 0.0f;
try
{
//Checks if the path is valid or not
if (!Directory.Exists(folder))
return folderSize;
else
{
try
{
foreach (string file in Directory.GetFiles(folder))
{
String path = @"\\?\" + file;
if (File.Exists(path))
{
FileInfo finfo = new FileInfo(path);
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);
}
}
}
catch (UnauthorizedAccessException e)
{
Console.WriteLine("Unable to calculate folder size: {0}", e.Message);
}
return folderSize;
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅 http://msdn.microsoft.com /en-us/library/aa365247(v=vs.85).aspx#maxpath 有关路径长度和
\\?\
前缀的信息。请参阅 http://msdn.microsoft.com/en -us/library/362314fe(v=vs.71).aspx 了解有关
@""
字符串的更多信息。它应该为您提供有关该主题的更多信息,至少足以提出更具体的问题。
See http://msdn.microsoft.com/en-us/library/aa365247(v=vs.85).aspx#maxpath about information on path lengths and the
\\?\
prefix.See http://msdn.microsoft.com/en-us/library/362314fe(v=vs.71).aspx for more information about
@""
strings.It should give you more information on the subject, at least enough to ask a more specific question.