c removes single line comment line examples from code

  • 2020-05-30 20:57:28
  • OfStack


using System.IO; 
using System.Collections; 
static string deleteComments(string s) // Get rid of to // Initial comment  
{ 
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); // Get rid of to // Initial comment  
brout.WriteLine(s); // Write out the  
Console.WriteLine(s); // Display on controls  
s = brin.ReadLine(); // Read in  
} 
brin.Close(); //  Close the connection between the buffered read stream and the file read stream . 
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"));
} 
}


Related articles: