-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
65 lines (53 loc) · 1.66 KB
/
Copy pathProgram.cs
File metadata and controls
65 lines (53 loc) · 1.66 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
using System.Text;
Console.Write("Enter the folder path: ");
string folderPath = Console.ReadLine() ?? string.Empty;
if (!Directory.Exists(folderPath))
{
Console.WriteLine("Folder does not exist!");
return;
}
string[] csFiles = Directory.GetFiles(folderPath, "*.cs", SearchOption.AllDirectories);
foreach (string file in csFiles)
{
string[] lines = File.ReadAllLines(file);
string? namespaceLine = lines.FirstOrDefault(line => line.Trim().EndsWith(";") && line.Trim().StartsWith("namespace "));
if (namespaceLine == null) continue;
string namespaceName = namespaceLine.Trim().Replace("namespace ", "").TrimEnd(';');
StringBuilder newContent = new StringBuilder();
bool foundNamespace = false;
bool inUsingBlock = true;
foreach (string line in lines)
{
string trimmedLine = line.Trim();
if (trimmedLine == namespaceLine.Trim())
{
newContent.AppendLine($"namespace {namespaceName}");
newContent.AppendLine("{");
foundNamespace = true;
inUsingBlock = false;
continue;
}
if (!foundNamespace)
{
newContent.AppendLine(line);
}
else
{
if (!string.IsNullOrWhiteSpace(line))
{
newContent.AppendLine(" " + line);
}
else
{
newContent.AppendLine();
}
}
}
if (foundNamespace)
{
newContent.AppendLine("}");
File.WriteAllText(file, newContent.ToString());
Console.WriteLine($"Modified: {file}");
}
}
Console.WriteLine("Processing complete!");