c#删除代码中的单行注释行示例
using System.IO;
using System.Collections;
static string deleteComments(string s) //去掉以//开始的注释
{
if (s == null) return s;
int pos = s.IndexOf("//");
if (pos < 0) return s;
return s.Substring(0, pos);
}
if (Directory.Exists("Temp"))
Directory.Delete("Temp");
if (File.Exists("Generator.cmd"))
File.Delete("Generator.cmd");
if (File.Exists("rem.txt"))
File.Delete("rem.txt");
Directory.CreateDirectory("Temp");
DirectoryInfo dir = new DirectoryInfo(".");
ArrayList folders = new ArrayList();
FileSystemInfo[] fileArr = dir.GetFileSystemInfos();
folders.AddRange(fileArr);
for (int i = 0; i < folders.Count; i++)
{
FileInfo f = folders[i] as FileInfo;
if (f == null)
{
DirectoryInfo d = folders[i] as DirectoryInfo;
if (!d.Name.Contains("Debug") && !d.Name.Contains("Release") && d.Name!="Temp")
{
Directory.CreateDirectory("Temp" + "\\" + d.Name);
folders.AddRange(d.GetFileSystemInfos());
}
}
else
{
string str = f.FullName;
if (Path.GetExtension(f.FullName) == ".c" || Path.GetExtension(f.FullName) == ".cpp" || Path.GetExtension(f.FullName) == ".h" || Path.GetExtension(f.FullName) == ".hpp" || Path.GetExtension(f.FullName) == ".cxx" || Path.GetExtension(f.FullName) == ".cs")
{
try
{
FileStream fin = new FileStream(
f.FullName, FileMode.Open, FileAccess.Read);
FileStream fout = new FileStream(
str.Replace(Directory.GetCurrentDirectory(), Directory.GetCurrentDirectory() + "\\Temp"), FileMode.Create, FileAccess.Write);
StreamReader brin = new StreamReader(
fin, System.Text.Encoding.Default);
StreamWriter brout = new StreamWriter(
fout, System.Text.Encoding.Default);
string s = brin.ReadLine();
while (s != null)
{
s = deleteComments(s); //去掉以//开始的注释
brout.WriteLine(s); //写出
Console.WriteLine(s); //在控制上显示
s = brin.ReadLine(); //读入
}
brin.Close(); // 关闭缓冲读入流及文件读入流的连接.
brout.Close();
}
catch (FileNotFoundException)
{
Console.WriteLine("File not found!");
}
catch (IOException e2)
{
Console.WriteLine(e2);
}
}
else
File.Copy(f.FullName, str.Replace(Directory.GetCurrentDirectory(), Directory.GetCurrentDirectory() + "\\Temp"));
}
}
最新评论