76 lines
1.7 KiB
C#
76 lines
1.7 KiB
C#
namespace Stream;
|
|
using System.IO;
|
|
|
|
public class Stream
|
|
{
|
|
public static bool ExistFile(string path)
|
|
{
|
|
return File.Exists(path);
|
|
}
|
|
|
|
public static void MyReplace(string path, char toReplace, char replace)
|
|
{
|
|
string preResult = String.Empty;
|
|
string result = String.Empty;
|
|
try
|
|
{
|
|
using (StreamReader sr = new StreamReader(path))
|
|
{
|
|
preResult = sr.ReadToEnd();
|
|
}
|
|
|
|
foreach (var chara in preResult)
|
|
{
|
|
if (chara == toReplace)
|
|
{
|
|
result += replace;
|
|
}
|
|
else
|
|
{
|
|
result += chara;
|
|
}
|
|
}
|
|
|
|
using (StreamWriter sw = new StreamWriter(path))
|
|
{
|
|
sw.WriteLine(result);
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
throw new ArgumentException("Error");
|
|
}
|
|
}
|
|
|
|
public static void DeleteLines(string path, int n)
|
|
{
|
|
string temp = String.Empty;
|
|
string final = String.Empty;
|
|
int i = 0;
|
|
try
|
|
{
|
|
using (StreamReader sr = new StreamReader(path))
|
|
{
|
|
string? line;
|
|
while ((line = sr.ReadLine()) != null)
|
|
{
|
|
if (i % n != 0)
|
|
{
|
|
final += line + "\n";
|
|
}
|
|
|
|
i++;
|
|
}
|
|
using (StreamWriter sw = new StreamWriter(path))
|
|
{
|
|
|
|
sw.Write(final);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception)
|
|
{
|
|
throw new ArgumentException("Error");
|
|
}
|
|
}
|
|
} |