2013-03-12 77 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

相关问题