1- using Common . BasicHelper . Utils ;
2- using Common . BasicHelper . Utils . Extensions ;
1+ using Common . BasicHelper . Utils . Extensions ;
32using System ;
43using System . IO ;
54using System . Threading . Tasks ;
65
7- #pragma warning disable IDE0051 // 删除未使用的私有成员
8-
96namespace Common . BasicHelper . IO ;
107
118public class FileHelper
@@ -15,30 +12,20 @@ public class FileHelper
1512 /// </summary>
1613 /// <param name="path">指定的路径</param>
1714 /// <param name="content">内容</param>
18- /// <returns>写入是否成功以及异常信息</returns>
19- public static Result < bool > WriteIn ( string path , string content )
15+ public static void WriteIn ( string path , string content )
2016 {
21- try
22- {
23- if ( File . Exists ( path ) ) File . Delete ( path ) ;
24-
25- File . Create ( path ) . Close ( ) ;
17+ if ( File . Exists ( path ) ) File . Delete ( path ) ;
2618
27- var fs = new FileStream ( path , FileMode . Open ) ;
28- var sw = new StreamWriter ( fs ) ;
19+ File . Create ( path ) . Close ( ) ;
2920
30- sw . Write ( content ) ;
31- sw . Flush ( ) ;
21+ var fs = new FileStream ( path , FileMode . Open ) ;
22+ var sw = new StreamWriter ( fs ) ;
3223
33- sw . CloseAndDispose ( ) ;
34- fs . CloseAndDispose ( ) ;
24+ sw . Write ( content ) ;
25+ sw . Flush ( ) ;
3526
36- return new Result < bool > ( true ) ;
37- }
38- catch ( Exception o )
39- {
40- throw new Result < bool > ( o . Message ) ;
41- }
27+ sw . CloseAndDispose ( ) ;
28+ fs . CloseAndDispose ( ) ;
4229 }
4330
4431 /// <summary>
@@ -54,29 +41,19 @@ public static void Append(string path, string content)
5441 /// </summary>
5542 /// <param name="path">路径</param>
5643 /// <param name="content">内容</param>
57- /// <returns>异常信息</returns>
58- public static Result < bool > WriteBytesTo ( string path , byte [ ] content )
44+ public static void WriteBytesTo ( string path , byte [ ] content )
5945 {
60- try
61- {
62- if ( ! File . Exists ( path ) )
63- File . Create ( path ) ;
46+ if ( ! File . Exists ( path ) )
47+ File . Create ( path ) ;
6448
65- var fs = new FileStream ( path , FileMode . Open ) ;
66- var bw = new BinaryWriter ( fs ) ;
67-
68- bw . Write ( content ) ;
69- bw . Flush ( ) ;
49+ var fs = new FileStream ( path , FileMode . Open ) ;
50+ var bw = new BinaryWriter ( fs ) ;
7051
71- bw . CloseAndDispose ( ) ;
72- fs . CloseAndDispose ( ) ;
52+ bw . Write ( content ) ;
53+ bw . Flush ( ) ;
7354
74- return new Result < bool > ( true ) ;
75- }
76- catch ( Exception p )
77- {
78- throw new Result < bool > ( p . Message ) ;
79- }
55+ bw . CloseAndDispose ( ) ;
56+ fs . CloseAndDispose ( ) ;
8057 }
8158
8259 /// <summary>
@@ -98,7 +75,7 @@ public static void WriteBytesToFile(string path, byte[] content)
9875 /// </summary>
9976 /// <param name="path">指定路径</param>
10077 /// <returns>内容或异常信息</returns>
101- public static string ReadAll ( string path )
78+ public static string ? ReadAll ( string path )
10279 {
10380 if ( File . Exists ( path ) )
10481 {
@@ -112,33 +89,25 @@ public static string ReadAll(string path)
11289
11390 return content ;
11491 }
115- else throw new Result < bool > ( "File didn't exists." ) ;
92+ else return null ;
11693 }
11794
11895 /// <summary>
11996 /// 异步读取指定路径的全部内容
12097 /// </summary>
12198 /// <param name="path">指定路径</param>
122- /// <returns>内容或异常信息</returns>
123- /// <exception cref="Result{bool}">异常</exception>
99+ /// <returns>内容</returns>
124100 public static async Task < string > ReadAllAsync ( string path )
125101 {
126- try
127- {
128- var fs = new FileStream ( path , FileMode . Open ) ;
129- var sr = new StreamReader ( fs ) ;
102+ var fs = new FileStream ( path , FileMode . Open ) ;
103+ var sr = new StreamReader ( fs ) ;
130104
131- var result = await sr . ReadToEndAsync ( ) ;
105+ var result = await sr . ReadToEndAsync ( ) ;
132106
133- sr . CloseAndDispose ( ) ;
134- fs . CloseAndDispose ( ) ;
107+ sr . CloseAndDispose ( ) ;
108+ fs . CloseAndDispose ( ) ;
135109
136- return result ;
137- }
138- catch ( Exception e )
139- {
140- throw new Result < bool > ( e . Message ) ;
141- }
110+ return result ;
142111 }
143112
144113 /// <summary>
@@ -183,50 +152,30 @@ public static void CreateFile(byte[] fileBuffer, string newFilePath)
183152 /// 将文件转换成 byte 数组
184153 /// </summary>
185154 /// <param name="fileUrl">文件路径文件名称</param>
186- /// <returns>byte 数组</returns>
187155 public static byte [ ] FileToBytes ( string fileUrl )
188156 {
189- try
190- {
191- var fs = new FileStream ( fileUrl , FileMode . Open , FileAccess . Read ) ;
192- var byteArray = new byte [ fs . Length ] ;
157+ var fs = new FileStream ( fileUrl , FileMode . Open , FileAccess . Read ) ;
158+ var byteArray = new byte [ fs . Length ] ;
193159
194- fs . Read ( byteArray , 0 , byteArray . Length ) ;
160+ fs . Read ( byteArray , 0 , byteArray . Length ) ;
195161
196- fs . CloseAndDispose ( ) ;
162+ fs . CloseAndDispose ( ) ;
197163
198- return byteArray ;
199- }
200- catch ( Exception e )
201- {
202- throw new Result < bool > ( e . Message ) ;
203- }
164+ return byteArray ;
204165 }
205166
206167 /// <summary>
207168 /// 将 byte 数组保存成文件
208169 /// </summary>
209170 /// <param name="byteArray">byte 数组</param>
210171 /// <param name="fileName">保存至硬盘的文件路径</param>
211- /// <returns>保存是否成功</returns>
212- public static Result < bool > ByteToFile ( byte [ ] byteArray , string fileName )
172+ public static void ByteToFile ( byte [ ] byteArray , string fileName )
213173 {
214- try
215- {
216- var fs = new FileStream ( fileName ,
217- FileMode . OpenOrCreate , FileAccess . Write ) ;
218-
219- fs . Write ( byteArray , 0 , byteArray . Length ) ;
174+ var fs = new FileStream ( fileName ,
175+ FileMode . OpenOrCreate , FileAccess . Write ) ;
220176
221- fs . CloseAndDispose ( ) ;
177+ fs . Write ( byteArray , 0 , byteArray . Length ) ;
222178
223- return new Result < bool > ( true ) ;
224- }
225- catch ( Exception e )
226- {
227- throw new Result < bool > ( e . Message ) ;
228- }
179+ fs . CloseAndDispose ( ) ;
229180 }
230- }
231-
232- #pragma warning restore IDE0051 // 删除未使用的私有成员
181+ }
0 commit comments