模拟获取份额大小的过程

发布于 2024-12-02 22:20:41 字数 2313 浏览 0 评论 0原文

我编写了一些代码来模拟获取共享驱动器大小的过程。驱动器来自文本文件。问题是路径达到了超过 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 技术交流群。

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

发布评论

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

评论(1

携君以终年 2024-12-09 22:20:41

请参阅 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.

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