Repository navigation
Expand file tree
/
Copy pathFileHelper.cs
More file actions
247 lines (237 loc) · 7.5 KB
/
Copy pathFileHelper.cs
File metadata and controls
247 lines (237 loc) · 7.5 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
namespace EasyFileOps;
public class FileHelper : IFileHelper
{
public async Task<OperationResult> ConvertTxtToHTML(string path)
{
try
{
var dotIndex = path.IndexOf('.');
if (dotIndex == -1) return new OperationResult() { IsSuccess = false, Message = "Invalid Path" };
var modifiedString = path.Substring(0, dotIndex + 1) + "html";
File.Move(path, modifiedString);
return new OperationResult()
{
IsSuccess = true,
Message = "SuccessFully Convert text to html"
};
}
catch(Exception ex)
{
return new OperationResult()
{
IsSuccess = false,
Message = ex.Message,
};
}
}
/// <summary>
/// convert text file to pdf
/// </summary>
/// <param name="path"></param>
/// <param name="fontFamily">name of font family</param>
/// <param name="fontSize">font size of text in pdf</param>
/// <param name="width">width of pdf</param>
/// <param name="height">height of pdf</param>
/// <returns>return is success and message </returns>
/// <exception cref="NotImplementedException"></exception>
public async Task<OperationResult> ConvertTxtToPDF(string PDfpath, string TxtPath, string fontFamily, float fontSize)
{
try
{
string text = File.ReadAllText(TxtPath);
PrintDocument printDocument = new PrintDocument();
printDocument.PrintPage += (sender, e) =>
{
e.Graphics.DrawString(text, new Font(fontFamily,fontSize), Brushes.Black, new RectangleF(0, 0, e.MarginBounds.Width, e.MarginBounds.Height));
};
using (var stream = new FileStream(PDfpath, FileMode.Create))
{
var writer = new PDFWriter(stream);
var result = writer.Write(printDocument);
writer.Dispose();
return new OperationResult()
{
IsSuccess = result.IsSuccess,
Message = result.Message
};
}
}
catch (Exception ex) {
Console.Error.WriteLine(ex.Message);
return new OperationResult()
{
IsSuccess = false,
Message = ex.Message
};
}
}
/// <summary>
/// the method that create file
/// </summary>
/// <param name="path">the path with name of file Example : C:/user/file.txt</param>
/// <returns>Return IsSuccess And Message</returns>
public OperationResult CreateFile(string path)
{
try
{
using (FileStream fs = File.Create(path));
return new OperationResult()
{
IsSuccess = true,
Message = "Successfully Create File"
};
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
return new OperationResult()
{
IsSuccess = false,
Message = ex.Message
};
}
}
/// <summary>
/// delete specefic file
/// </summary>
/// <param name="path">string path with name of file </param>
/// <returns>return IsSuccess And Message</returns>
public OperationResult DeleteFile(string path)
{
try
{
File.Delete(path);
return new OperationResult()
{
IsSuccess = true,
Message = "File Successfully deleted"
};
}
catch(Exception ex)
{
Console.Error.WriteLine(ex.Message);
return new OperationResult()
{
IsSuccess = false,
Message = ex.Message
};
}
}
/// <summary>
/// move file to another directory
/// </summary>
/// <param name="source">path of source of file</param>
/// <param name="destination">path of destination of file</param>
/// <returns>return IsSuccess And Message</returns>
public OperationResult MoveFile(string source, string destination)
{
try
{
Directory.Move(source, destination);
return new OperationResult()
{
IsSuccess = true,
Message = $"Successfully File moved from {source} to {destination}",
};
}catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
return new OperationResult()
{
IsSuccess = false,
Message = ex.Message,
};
}
}
/// <summary>
/// Rename file
/// </summary>
/// <param name="Absoultepath">the path of file with out name </param>
/// <param name="oldName">old name of file </param>
/// <param name="newName">new name of file</param>
/// <returns></returns>
public OperationResult RenameFile(string Absoultepath,string oldName, string newName)
{
try
{
File.Move(Path.Combine(Absoultepath,oldName), Path.Combine(Absoultepath, newName));
return new OperationResult()
{
IsSuccess = true,
Message = "File Successfully Renamed"
};
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
return new OperationResult()
{
IsSuccess = false,
Message = ex.Message
};
}
}
/// <summary>
/// write content to the file and option to choose overwrite or append content
/// </summary>
/// <param name="path">path of file</param>
/// <param name="content">content of file</param>
/// <param name="IsAppend">IsAppend if be true the content add to the existing content otherwise overwrite it</param>
/// <returns></returns>
public OperationResult WriteLine(string path, StringBuilder content,bool IsAppend)
{
try
{
using (var streamWriter = new StreamWriter(path,IsAppend))
{
streamWriter.WriteLine(content);
}
return new OperationResult()
{
IsSuccess = true,
Message = "File Successfully Writed",
};
}
catch(Exception ex)
{
Console.Error.WriteLine(ex.Message);
return new OperationResult()
{
IsSuccess = false,
Message = ex.Message
};
}
}
public async Task<OperationResult> WriteLineAsync(string path, StringBuilder content, bool IsAppend = true)
{
try
{
using (var streamWriter = new StreamWriter(path, IsAppend))
{
await streamWriter.WriteLineAsync(content);
}
return new OperationResult()
{
IsSuccess = true,
Message = "File Successfully Writed",
};
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.Message);
return new OperationResult()
{
IsSuccess = false,
Message = ex.Message
};
}
}
}