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"));
}
}
上一篇: Java 爬虫工具Jsoup详解
下一篇: c#连接access数据库操作类分享