2013-03-12 76 views
0

我想創建一個哈希文本文件。代碼起作用,問題在於,一旦Streamwriter啓動進程,它將不會停止,直到完成。我想將輸出文件分解成更小的部分。如何在不重新啓動流程的情況下停止Streamwriter並啓動新文件?C#streamwriter創建文件的一部分

string infile = @"ntlmchar.txt"; 
     string hashfile = @"ntlmhash.txt"; //File that includes the hash and clear test 
     string charfile = @"ntlmchar.txt"; //File that only has the clear text 
     string oldCharFile = ""; //Temp file to apply to infile. 
     int cint = 1; //The number of characters in the file 
     string str_cint = cint.ToString(); //convert cint to string 
     int pint = 1; //The number of parts to the character file 
     string str_pint = pint.ToString(); //convert pint to string 
     int cm = 4; //Max number of characters 
     int pm = 4000; //Man number of parts 
     int line = 0; //line index number 

     while (cint <= cm) 
     { 
      if (!File.Exists(infile)) 
      { 
       for (int ci =1; ci <= cm; ci++) 
       { 
        str_cint = cint.ToString(); 
        for (int pi =1; pi <= pm; pi++) 
        { 
         str_pint = pint.ToString(); 
         // System.Console.WriteLine("Inner for loop cint file does not exist" +cint +" pint " + pint); 
        // System.Console.WriteLine("Inner for loop str_cint file does not exist " + str_cint + " cint " + cint); 
         charfile = "ntlmchar" + str_cint + "_" + str_pint + ".txt"; 
         pint = pi; 
         oldCharFile = charfile; 
         infile = oldCharFile; 
         if (File.Exists(infile)) break; 
        // System.Console.WriteLine("inner loop file " + infile); 
        } 
       //  System.Console.WriteLine("outer for loop cint " + cint + " pint " + pint);    
        // System.Console.WriteLine("infile not found " + infile + " " + oldCharFile + " " + charfile + " " + hashfile); 
       } 
       // System.Console.WriteLine("No work files found " + infile + " " + oldCharFile + " " + charfile + " " + hashfile); 
      } 

      else if (File.Exists(infile)) 
      { 
       // Create a file to write to. 
      // System.Console.WriteLine("cint at the start of else if " + cint + " str_cint " + str_cint); 
       infile = oldCharFile; 
       str_cint = cint.ToString(); 
     //  System.Console.WriteLine("cint after assign to str_cint " + cint + " str_cint " + str_cint); 
       pint=1; 
       str_pint = pint.ToString(); 
       hashfile = "ntlmhash" + str_cint + "_" + str_pint + ".txt"; 
       charfile = "ntlmchar" + str_cint + "_" + str_pint + ".txt"; 
       //System.Console.WriteLine(infile + " " + oldCharFile + " " + charfile + " " + hashfile); 
     //   System.Console.WriteLine("Infile found " + cint + " " + pint); 
       using (StreamWriter h = new StreamWriter(hashfile)) 
       using (StreamWriter c = new StreamWriter(charfile)) 
       using (StreamReader sr = new StreamReader(infile)) 
       { 
        string i = ""; 
        while ((i = sr.ReadLine()) != null) 
        { 
         foreach (string s in alpha) 
         { 

          if (line <= 2000000) 
          { 
           string j = i + s; 
           string str = Program.Ntlm(j); 
           hashfile = "ntlmhash" + str_cint + "_" + str_pint + ".txt"; 
           charfile = "ntlmchar" + str_cint + "_" + str_pint + ".txt"; 
         //   System.Console.WriteLine("line before writing to file " + line + " in charfile " + charfile); 
           h.WriteLine("{0}, {1}", j, str); 
           c.WriteLine("{0}", j); 
           line++; 

         //  System.Console.WriteLine("h file" + h + " c file" + c); 

          } 
          else 
          { 
           h.Flush(); 
           c.Flush(); 
           pint++; 
           str_pint = pint.ToString(); 
           hashfile = "ntlmhash" + str_cint + "_" + str_pint + ".txt"; 
           charfile = "ntlmchar" + str_cint + "_" + str_pint + ".txt"; 
           line = 1; 
           System.Console.WriteLine("line after writing to part of file " + line + " in charfile " + charfile); 
          } 

         } 
        } 
+0

應該在輸出部分能獨立解碼? – 2013-03-12 13:01:34

+0

是的,我打算將文本文件導入到mysql,希望通過php。 – user2087542 2013-03-13 14:24:07

回答

1

我假設你試圖爲每個文件獲得2,000,000個項目?你只需要重新調整一下。

現在您有:

using (StreamWriter h = new StreamWriter(hashfile)) 
using (StreamWriter c = new StreamWriter(charfile)) 
using (StreamReader sr = new StreamReader(infile)) 
{ 
    string i = ""; 
    while ((i = sr.ReadLine()) != null) 
    { 

你需要改變你的代碼,這樣你以後打開輸出文件:

using (StreamReader sr = new StreamReader(infile)) 
{ 
    StreamWriter h = null; 
    StreamWriter c = null; 
    try 
    { 
     h = new StreamWriter(...); 
     c = new StreamWriter(...); 
     string i = ""; 
     while ((i = sr.ReadLine()) != null) 
     { 
      // output line here 
      // and increment line counter. 
      ++line; 
      if (line > 2000000) 
      { 
       // Close the output files and open new ones 
       h.Close(); 
       c.Close(); 
       h = new StreamWriter(...); 
       c = new StreamWriter(...); 
       line = 1; 
      } 
     } 
    } 
    finally 
    { 
     if (h != null) h.Close(); 
     if (c != null) c.Close(); 
    } 
} 
+0

謝謝,這有幫助。我不得不修改它,但它的工作。我希望我能粘貼代碼。無論如何,第二階段,FTP,然後使用PHP將文件導入到MySQL。我將爲這些主題創建另一個線程。 – user2087542 2013-03-15 02:51:54

+0

@ user2087542:很高興你的工作。如果此答案解決了您的問題,請將其標記爲接受的答案(單擊複選標記)。 – 2013-03-15 10:02:23

相關問題