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))
开发者_如何学Go {
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;
}
}
}
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.
精彩评论