Advertisement
WilleMahMille

Exempel - Filhantering

Jan 24th, 2020
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.16 KB | None | 0 0
  1. namespace Filhantering {
  2.     class Program {
  3.        
  4.         static void Main(string[] args) {
  5.             string path = "C:\\Users\\Wilhelm.jansson2\\Source\\Repos\\CSharp Test\\CSharp Test\\test.txt";
  6.             if(args.Length > 0) {
  7.                 path = args[0];
  8.             }
  9.             //args[0] is the filepath
  10.             StreamReader fReader = new StreamReader(path);
  11.             StreamWriter fWriter = new StreamWriter(path.Substring(0, path.Length - 4) + " - out.txt");
  12.  
  13.             bool inComment = false;
  14.             while (!fReader.EndOfStream) {
  15.                 string finalString = "";
  16.                 string temp = fReader.ReadLine();
  17.  
  18.                 int startIndex = 0;
  19.                 int finalLength = inComment ? 0 : temp.Length;
  20.                
  21.                 for (int i = 0; i < temp.Length - 1; i++) {
  22.                     if (temp.Substring(i, 2) == "//" && !inComment) {
  23.                         finalLength = finalLength > i - startIndex ? i - startIndex : finalLength;
  24.                         break;
  25.                     }
  26.                     if (temp.Substring(i, 2) == "/*" && !inComment) {
  27.                         inComment = true;
  28.                         if(finalLength > i - startIndex) {
  29.                             string sTemp = temp.Substring(startIndex, i - startIndex);
  30.                             finalString += temp.Substring(startIndex, i - startIndex);
  31.                             finalLength = i - startIndex;
  32.                         }
  33.                         i += 2;
  34.                        
  35.                     }
  36.                     if (temp.Substring(i, 2) == "*/" && inComment) {
  37.                         inComment = false;
  38.                         startIndex = i + 2;
  39.                         finalLength = temp.Length - startIndex;
  40.                         i += 1;
  41.                     }
  42.                 }
  43.                
  44.                 finalString += temp.Substring(startIndex, finalLength);
  45.                 fWriter.WriteLine(finalString);
  46.             }
  47.             fReader.Close();
  48.             fWriter.Close();
  49.             Console.WriteLine("Done");
  50.             Console.ReadKey(true);
  51.         }
  52.  
  53.  
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement