-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
127 lines (97 loc) · 3.06 KB
/
Program.cs
File metadata and controls
127 lines (97 loc) · 3.06 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
// using System.IO; // to use StreamWriter
using System.Diagnostics; //to use linux bash(shellExec)
namespace TextEditor;
class Program
{
public static string savePath = "/mnt/c/dev/c#/TextEditor/saves/"; //global variable to static method
static void Main(string[] args)
{
Menu();
}
static void Menu() {
Console.Clear();
Console.WriteLine("\n-----------------------------------------");
Console.WriteLine("| O que você deseja fazer? |");
Console.WriteLine("| 1 - Abrir arquivo |");
Console.WriteLine("| 2 - Criar novo arquivo |");
Console.WriteLine("| 0 - sair |");
Console.WriteLine("-----------------------------------------");
Console.Write("===> ");
short option = short.Parse(Console.ReadLine());
switch (option) {
case 0:
System.Environment.Exit(0);
break;
case 1:
OpenFile();
break;
case 2:
MakeFile();
break;
default:
Menu();
break;
}
}
static void OpenFile() {
Console.Clear();
Console.WriteLine("Digite o nome do arquivo que deseja editar:");
string fileName = Console.ReadLine();
using(var file = new StreamReader($"{savePath}{fileName}.txt")) {
string text = file.ReadToEnd();
Console.WriteLine(text);
}
Console.WriteLine("");
Console.ReadLine();
Menu();
}
static void MakeFile() {
Console.Clear();
Console.WriteLine("Digite seu texto abaixo: (ESC para sair)");
Console.WriteLine("------------------------");
string text = "";
// Console.Read();
do {
text += Console.ReadLine();
text += Environment.NewLine;
}
while(Console.ReadKey().Key != ConsoleKey.Escape);
// Console.Write(text);
SaveFile(text);
}
static void SaveFile(string text) {
Console.Clear();
Console.WriteLine("Digite o nome do arquivo de texto:");
Console.Write("===>");
string fileName = Console.ReadLine();
if (!Directory.Exists(savePath)) {
Shellexec($"rm -r {savePath.TrimEnd('/')} 2> /dev/null");
DirectoryInfo dir = Directory.CreateDirectory(savePath);
Console.WriteLine("Pasta criada com sucesso em {0}.", Directory.GetCreationTime(savePath));
}
//Add validation if file exists
//here
//Close files automatically
using(var file = new StreamWriter($"{savePath}{fileName}.txt")) {
file.WriteLine(text);
}
Console.WriteLine("Arquivo salvo com sucesso!");
Thread.Sleep(2500);
Menu();
}
static void Shellexec(string comando) {
// Criação de um processo
Process processo = new Process();
processo.StartInfo.FileName = "/bin/bash"; // Shell no Linux
processo.StartInfo.Arguments = $"-c \"{comando}\""; // Passando o comando como argumento
processo.StartInfo.RedirectStandardOutput = true; // Redirecionando a saída padrão
processo.StartInfo.UseShellExecute = false; // Não usar o shell padrão
processo.StartInfo.CreateNoWindow = true; // Não criar janela de console
// Iniciar o processo e aguardar a conclusão
processo.Start();
// string resultado = processo.StandardOutput.ReadToEnd();
processo.WaitForExit();
// Exibir a saída do comando
// Console.WriteLine(resultado);
}
}