Skip to content

Commit ce83614

Browse files
new: SimpleFileHandler.ProjectToLocation for extracting an existing file from the project to a given location
1 parent fcfa5a1 commit ce83614

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,13 @@ string text = SimpleFileHandler.Read("path/to/file.txt");
176176
- appends text to a file
177177
```csharp
178178
SimpleFileHandler.Append("path/to/file.txt", "Sample text 2");
179+
```
180+
### ProjectToLocation
181+
- extract a file from the project to a given location
182+
```csharp
183+
SimpleFileHandler.ProjectToLocation("SampleClass.cs");
184+
185+
// Or
186+
187+
SimpleFileHandler.ProjectToLocation("SampleClass.cs", "path/to/destination");
179188
```

SimpleFileHandler.cs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.IO;
2+
using System.Reflection;
23

34
namespace CSSimpleFunctions
45
{
@@ -18,5 +19,41 @@ public static void Append(string FilePath, string Content)
1819
{
1920
File.AppendAllText(FilePath, Content);
2021
}
22+
23+
public static void ProjectToLocation(string FileName)
24+
{
25+
try
26+
{
27+
if (!Path.GetDirectoryName(FileName).Equals(string.Empty) && !Directory.Exists(Path.GetDirectoryName(FileName)))
28+
{
29+
Directory.CreateDirectory(Path.GetDirectoryName(FileName));
30+
}
31+
FileStream ProjectFileStream = File.Create(FileName);
32+
Assembly.GetExecutingAssembly().GetManifestResourceStream(Assembly.GetCallingAssembly().EntryPoint.DeclaringType.Namespace + "." + Path.GetFileName(FileName)).CopyTo(ProjectFileStream);
33+
ProjectFileStream.Close();
34+
}
35+
catch
36+
{
37+
Console.WriteLine("Cannot copy project file. Please make sure the file's build action is set to 'Embedded Resource'.");
38+
}
39+
}
40+
41+
public static void ProjectToLocation(string FileName, string FilePath)
42+
{
43+
try
44+
{
45+
if (!Directory.Exists(FilePath))
46+
{
47+
Directory.CreateDirectory(FilePath);
48+
}
49+
FileStream ProjectFileStream = File.Create(Path.Combine(FilePath, Path.GetFileName(FileName)));
50+
Assembly.GetExecutingAssembly().GetManifestResourceStream(Assembly.GetCallingAssembly().EntryPoint.DeclaringType.Namespace + "." + Path.GetFileName(FileName)).CopyTo(ProjectFileStream);
51+
ProjectFileStream.Close();
52+
}
53+
catch
54+
{
55+
Console.WriteLine("Cannot copy project file. Please make sure the file's build action is set to 'Embedded Resource'.");
56+
}
57+
}
2158
}
2259
}

0 commit comments

Comments
 (0)