欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

c#删除代码中的单行注释行示例

程序员文章站 2024-02-24 17:34:00
复制代码 代码如下:using system.io; using system.collections; static string deletecomments(st...

复制代码 代码如下:

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"));
}
}