管道基础大数据平台系统开发-【CS】-ExportMap
1
13693261870
2024-09-04 dbff3d984eb99f474ff63ee6daaadc2c00eee620
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace CountFiles
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }
 
        private void btnCount_Click(object sender, EventArgs e)
        {
            try
            {
                string path = this.txtPath.Text.Trim();
                if (string.IsNullOrEmpty(path))
                {
                    MessageBox.Show("请选择一个目录!");
                    this.txtPath.Focus();
                }
                if (!Directory.Exists(path))
                {
                    MessageBox.Show("你选择的目录不存在!");
                    this.txtPath.Focus();
                }
 
                string[] files = Directory.GetFiles(path, "*", SearchOption.AllDirectories);
 
                List<string> list = new List<string>();
                foreach (string file in files)
                {
                    //sizes += new FileInfo(file).Length;
                    FileInfo fi = new FileInfo(file);
                    list.Add(file + "," + FormatBytes(fi.Length));
                }
 
                string rs = Path.Combine(path, "统计结果.txt");
                File.WriteAllLines(rs, list.ToArray());
 
                MessageBox.Show("计算完成!");
                OpenFolder(path);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "出错");
            }
        }
 
        public static string FormatBytes(long bytes)
        {
            string[] Suffix = { "Byte", "KB", "MB", "GB", "TB" };
 
            int i = 0;
            double dblSByte = bytes;
            if (bytes > 1024)
                for (i = 0; (bytes / 1024) > 0; i++, bytes /= 1024)
                    dblSByte = bytes / 1024.0;
 
            return String.Format("{0:0.##} {1}", dblSByte, Suffix[i]);
        }
 
        private static void OpenFolder(string folderPath)
        {
            if (string.IsNullOrEmpty(folderPath)) return;
 
            Process process = new Process();
            ProcessStartInfo psi = new ProcessStartInfo("Explorer.exe");
            psi.Arguments = folderPath;
            process.StartInfo = psi;
 
            try
            {
                process.Start();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                process.Close();
            }
        }
    }
}