|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 using System; |
|
18 using System.Text; |
|
19 using System.IO; |
|
20 using System.Collections; |
|
21 |
|
22 namespace SymbianUtils.FileSystem.Utilities |
|
23 { |
|
24 public static class FSUtilities |
|
25 { |
|
26 #region Constants |
|
27 public const int KKiloByte = 1024; |
|
28 public const int KMegaByte = KKiloByte * KKiloByte; |
|
29 #endregion |
|
30 |
|
31 #region API |
|
32 public static bool Exists( string aFileName ) |
|
33 { |
|
34 bool ret = false; |
|
35 // |
|
36 try |
|
37 { |
|
38 ret = File.Exists( aFileName ); |
|
39 } |
|
40 catch ( Exception ) |
|
41 { |
|
42 } |
|
43 // |
|
44 return ret; |
|
45 } |
|
46 |
|
47 public static void DeleteFile( string aFileName ) |
|
48 { |
|
49 try |
|
50 { |
|
51 if ( File.Exists( aFileName ) ) |
|
52 { |
|
53 FileAttributes attribs = File.GetAttributes( aFileName ); |
|
54 attribs &= ~FileAttributes.ReadOnly; |
|
55 attribs &= ~FileAttributes.System; |
|
56 attribs &= ~FileAttributes.Hidden; |
|
57 File.SetAttributes( aFileName, attribs ); |
|
58 |
|
59 File.Delete( aFileName ); |
|
60 } |
|
61 } |
|
62 finally |
|
63 { |
|
64 } |
|
65 } |
|
66 |
|
67 public static string PrettyFileFormatSize( long aValue, bool aExtraRounding ) |
|
68 { |
|
69 string ret = string.Empty; |
|
70 |
|
71 if ( aValue < 1024000 ) // If < 1000K |
|
72 { |
|
73 long sizeInK = 0; |
|
74 |
|
75 if ( aValue != 0 ) |
|
76 { |
|
77 sizeInK = (long) ( (aValue + 512) >> 10 ); |
|
78 |
|
79 if (sizeInK < 1) |
|
80 { |
|
81 sizeInK = 1; |
|
82 } |
|
83 if (sizeInK > 999) |
|
84 { |
|
85 sizeInK = 999; |
|
86 } |
|
87 } |
|
88 |
|
89 ret = sizeInK.ToString() + "K"; |
|
90 } |
|
91 else |
|
92 { |
|
93 double sizeInM = (double) aValue; |
|
94 sizeInM /= (double) KMegaByte; |
|
95 if ( sizeInM < 1 ) |
|
96 { |
|
97 sizeInM = 1; |
|
98 } |
|
99 |
|
100 string postfix = "M"; |
|
101 if ( sizeInM >= 1000 ) |
|
102 { |
|
103 sizeInM /= 1024; // Size in G |
|
104 if (sizeInM < 1) |
|
105 { |
|
106 sizeInM = 1; |
|
107 } |
|
108 |
|
109 postfix = "G"; |
|
110 } |
|
111 |
|
112 if ( sizeInM > 999.9) |
|
113 { |
|
114 sizeInM = 999.9; |
|
115 } |
|
116 |
|
117 if ( aExtraRounding ) |
|
118 { |
|
119 sizeInM += 0.499999; |
|
120 } |
|
121 |
|
122 ret = sizeInM.ToString( "##############.00" ) + postfix; |
|
123 } |
|
124 |
|
125 return ret; |
|
126 } |
|
127 |
|
128 public static string FormatFileSize( long aSize ) |
|
129 { |
|
130 string ret = ""; |
|
131 if ( aSize > KMegaByte ) |
|
132 { |
|
133 ret = aSize.ToString("###,000,000"); |
|
134 } |
|
135 else if ( aSize > 1024 ) |
|
136 { |
|
137 ret = aSize.ToString("###,000"); |
|
138 } |
|
139 else |
|
140 { |
|
141 ret += aSize.ToString("###"); |
|
142 } |
|
143 return ret; |
|
144 } |
|
145 |
|
146 public static string MakeTempFileName( string aPath ) |
|
147 { |
|
148 string tempFileName = string.Empty; |
|
149 // |
|
150 while( tempFileName == string.Empty || File.Exists( tempFileName ) ) |
|
151 { |
|
152 DateTime date = DateTime.Now; |
|
153 // |
|
154 tempFileName = "temp_file_" + date.Minute.ToString("d2") + "_" + date.Ticks.ToString("d6") + ".tmp"; |
|
155 tempFileName = Path.Combine( aPath, tempFileName ); |
|
156 // |
|
157 System.Threading.Thread.Sleep( 5 ); |
|
158 } |
|
159 // |
|
160 return tempFileName; |
|
161 } |
|
162 |
|
163 public static string StripAllExtensions( string aFileName ) |
|
164 { |
|
165 string temp = Path.GetFileNameWithoutExtension( aFileName ); |
|
166 string ret = string.Empty; |
|
167 // |
|
168 do |
|
169 { |
|
170 ret = temp; |
|
171 temp = Path.GetFileNameWithoutExtension( ret ); |
|
172 } |
|
173 while( temp != ret ); |
|
174 |
|
175 return ret; |
|
176 } |
|
177 |
|
178 public static void ClearTempPath() |
|
179 { |
|
180 try |
|
181 { |
|
182 string path = TempPathBaseDir; |
|
183 DirectoryInfo dir = new DirectoryInfo( path ); |
|
184 // |
|
185 if ( dir.Exists ) |
|
186 { |
|
187 dir.Delete( true ); |
|
188 } |
|
189 } |
|
190 catch ( Exception ) |
|
191 { |
|
192 } |
|
193 } |
|
194 |
|
195 public static string MakeTempPath() |
|
196 { |
|
197 string ret = string.Empty; |
|
198 string temp = TempPathBaseDir; |
|
199 // |
|
200 while ( ret == string.Empty ) |
|
201 { |
|
202 try |
|
203 { |
|
204 string tempPathExtension = SymbianUtils.Strings.StringUtils.MakeRandomString(); |
|
205 string path = Path.Combine( temp, tempPathExtension ) + Path.DirectorySeparatorChar; |
|
206 // |
|
207 DirectoryInfo dir = new DirectoryInfo( path ); |
|
208 if ( !dir.Exists ) |
|
209 { |
|
210 dir.Create(); |
|
211 ret = path; |
|
212 } |
|
213 } |
|
214 catch ( Exception ) |
|
215 { |
|
216 } |
|
217 } |
|
218 // |
|
219 return ret; |
|
220 } |
|
221 #endregion |
|
222 |
|
223 #region Internal properties |
|
224 private static string TempPathBaseDir |
|
225 { |
|
226 get |
|
227 { |
|
228 string temp = Path.Combine( Path.GetTempPath(), KTempPathFolder ); |
|
229 return temp; |
|
230 } |
|
231 } |
|
232 #endregion |
|
233 |
|
234 #region Internal constants |
|
235 private const string KTempPathFolder = "SymbianNetTools"; |
|
236 #endregion |
|
237 } |
|
238 } |