亲宝软件园·资讯

展开

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

人气:0
想了解c#删除代码中的单行注释行示例的相关内容吗,在本文为您仔细讲解c#删除注释行的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:单行注释,下面大家一起来学习吧。

复制代码 代码如下:

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

加载全部内容

相关教程
猜你喜欢
用户评论