-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cs
More file actions
34 lines (32 loc) · 1.08 KB
/
Utils.cs
File metadata and controls
34 lines (32 loc) · 1.08 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
using ClosedXML.Excel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace WebVella.DocumentTemplates.ExcelExample;
public class Utils
{
public MemoryStream LoadFileAsStream(string fileName)
{
var path = Path.Combine(Environment.CurrentDirectory, $"{fileName}");
var fi = new FileInfo(path);
if (!fi.Exists) throw new FileNotFoundException();
MemoryStream ms = new MemoryStream();
using (FileStream file = new FileStream(path, FileMode.Open, FileAccess.Read))
file.CopyTo(ms);
return ms;
}
public void SaveFileFromStream(MemoryStream content, string fileName)
{
DirectoryInfo? debugFolder = Directory.GetParent(Environment.CurrentDirectory);
if (debugFolder is null) throw new Exception("debugFolder not found");
var projectFolder = debugFolder.Parent?.Parent;
if (projectFolder is null) throw new Exception("projectFolder not found");
var path = Path.Combine(projectFolder.FullName,fileName);
using (FileStream fs = new FileStream(path, FileMode.Create))
{
content.WriteTo(fs);
}
}
}