-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathHelper.cs
142 lines (125 loc) · 4.46 KB
/
Helper.cs
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.IO;
using System.Windows.Forms;
namespace csharp_File_Shredder
{
static class Helper
{
public static ArrayList GetListViewElements(ListView lv, int i)
{
ArrayList ret = new ArrayList();
foreach (ListViewItem lvi in lv.Items) {
if (i < lvi.SubItems.Count) {
ret.Add(lvi.SubItems[i].Text);
}
}
return ret;
}
public static double GetFileSizeInBytesFromFileList(ArrayList files)
{
double return_value = 0;
if (files.Count > 0) {
foreach (string file in files) {
FileInfo fileInfo = new FileInfo(file);
return_value += fileInfo.Length;
}
}
return return_value;
}
public static string FormatedFileSize(string fileName)
{
try {
FileInfo fileInfo = new FileInfo(fileName);
double fileSize = (fileInfo.Length / 1024.0);
if (fileSize < 1024) {
return fileSize.ToString("F01") + " kB";
} else {
double tempFileSize = fileSize / 1024.0;
if (tempFileSize < 1024) {
return tempFileSize.ToString("F01") + " MB";
} else {
tempFileSize /= 1024;
return tempFileSize.ToString("F01") + " GB";
}
}
} catch (System.Exception ex) {
MessageBox.Show(ex.Message);
}
return "0kB";
}
public static bool IsDirectory(string path)
{
if (Directory.Exists(path)) {
return true;
}
return false;
}
public static string FormatKiloBytes(double size)
{
if (size < 1024) {
return size.ToString("F01") + " kB";
} else {
double tempSize = size / 1024.0;
if (tempSize < 1024) {
return tempSize.ToString("F01") + " MB";
} else {
tempSize /= 1024;
return tempSize.ToString("F01") + " GB";
}
}
}
public static string FormatMiliseconds(double miliseconds)
{
if (miliseconds < 1000) {
return miliseconds.ToString("F01") + " ms";
} else {
double tempMiliseconds = miliseconds / 1000;
if (tempMiliseconds < 60) {
return tempMiliseconds.ToString("F01") + " s";
} else {
tempMiliseconds /= 60;
if (tempMiliseconds < 60) {
return tempMiliseconds.ToString("F01") + " m";
} else {
tempMiliseconds /= 60;
return tempMiliseconds.ToString("F01") + " h";
}
}
}
}
public static string FormatMBps(double MBps)
{
return MBps.ToString("F01") + " MBps";
}
public static ArrayList GetFileNamesInDirectory(String Path, bool Recursive, bool FullPath)
{
ArrayList files = new ArrayList();
DirectoryInfo directory = new DirectoryInfo(Path);
if (directory.Exists) {
foreach (FileInfo file in directory.GetFiles()) {
if (FullPath) {
if (!files.Contains(Path + "\\" + file.Name)) {
files.Add(Path + "\\" + file.Name);
} else if (!files.Contains(file.Name)) {
files.Add(file.Name);
}
}
}
if (Recursive) {
foreach (DirectoryInfo subDirectory in directory.GetDirectories()) {
foreach (Object o in GetFileNamesInDirectory(Path + "\\" + subDirectory.Name, true, FullPath)) {
if (!files.Contains(o.ToString())) {
files.Add(o.ToString());
}
}
}
}
}
return files;
}
}
}