2015-01-21 3 views
-1

Я хочу записать номера всех номеров банков в фактический файл. Не консольная версия. Но я не вижу фактические файлы в файловой системе. Как записать числа в файловую систему?Запись в файловую систему не консоль

У меня есть это:

static void Main(string[] args) 
     { 



      string path = @"G:\Folder"; 

      //string fileName = string.Format("{0}{1}-", part[0], part[part.Count - 1]); 
      var bans = BankAcoutNumbers.BANS;   
      const int MAX_FILES = 10; 
      const int BANS_PER_FILE = 10; 
      int bansCounter = 0; 
      var part = new List<int>(); 
      //string fileName = string.Format("{0}-{1}", part[0], part[part.Count - 1]); 

      var maxNumberOfFiles = 10; 
      Stopwatch timer = new Stopwatch(); 



       var fileCounter = 0; 

       if (!Directory.Exists(path)) 
       { 
        DirectoryInfo di = Directory.CreateDirectory(path); 
       } 

       //var destiNationFile = new StreamWriter(string.Format(fileName, fileCounter + 1)); 
       try 
       { 

        // foreach (var bank in BankAcoutNumbers.BANS.Take(100)) 
        //{ 


        while (fileCounter <= maxNumberOfFiles) 
        { 
         timer.Start(); 
         foreach (var bank in BankAcoutNumbers.BANS) 
         { 
          part.Add(bank); 
          if(++bansCounter >= BANS_PER_FILE) 
          { 
           string fileName = string.Format("{0}-{1}", part[0], part[part.Count - 1]); 
           //var destinationFile = new StreamWriter(fileName); 
           //destiNationFile = new StreamWriter(fileName); 
           Console.WriteLine("NR{0}", fileName); 
           foreach (var partBan in part)         
            Console.WriteLine(partBan); 
           part.Clear(); 
           bansCounter = 0; 

           if (++fileCounter >= MAX_FILES) 
           break; 



           //lineCounter = 0; 
           //destiNationFile.Flush();         
           //destiNationFile.Dispose(); 
           //destiNationFile = new StreamWriter(string.Format(fileName, fileCounter + 1)); 
           //fileCounter++; 
          } 

          //destiNationFile.WriteLine(bank); 
          //lineCounter++;     
         } 
         //fileCounter++;       
         //} 
        }      
        timer.Stop(); 

        Console.WriteLine(timer.Elapsed.Seconds); 
       } 
       catch (Exception) 
       { 

        throw; 
       } 



      // Keep the console window open in debug mode. 

      System.Console.WriteLine("Press any key to exit."); 
      System.Console.ReadKey(); 
     } 

Спасибо

public class Program 
    { 
     //BankAcoutNumbers bankAccountNumbers = new BankAcoutNumbers(); 
     static void Main(string[] args) 
     { 



      string path = @"G:\Folder"; 

      //string fileName = string.Format("{0}{1}-", part[0], part[part.Count - 1]); 
      var bans = BankAcoutNumbers.BANS;   
      const int MAX_FILES = 10; 
      const int BANS_PER_FILE = 10; 
      int bansCounter = 0; 
      var part = new List<int>(); 
      //part.Add(456456465); 
      //string fileName = string.Format("{0}-{1}", part[0], part[part.Count - 1]); 

      var maxNumberOfFiles = 10; 
      Stopwatch timer = new Stopwatch(); 



       var fileCounter = 0; 

       if (!Directory.Exists(path)) 
       { 
        DirectoryInfo di = Directory.CreateDirectory(path); 
       } 

       //var destiNationFile = new StreamWriter(string.Format(fileName, fileCounter + 1)); 
       try 
       { 

        // foreach (var bank in BankAcoutNumbers.BANS.Take(100)) 
        //{ 


        while (fileCounter <= maxNumberOfFiles) 
        { 
         timer.Start(); 
         foreach (var bank in BankAcoutNumbers.BANS) 
         { 
          part.Add(bank); 
          if(++bansCounter >= BANS_PER_FILE) 
          { 
           string fileName = string.Format("{0}-{1}", part[0], part[part.Count - 1]); 
           //var destinationFile = new StreamWriter(fileName); 
           //destiNationFile = new StreamWriter(fileName); 
           Console.WriteLine("NR{0}", fileName); 
           fileName = @"G:\\Folder" + fileName; 

           foreach (var partBan in part) 
           { 
            Console.WriteLine(partBan); 
            System.IO.File.WriteAllText(fileName, partBan.ToString()); 
           } 
           part.Clear(); 
           bansCounter = 0; 

           if (++fileCounter >= MAX_FILES) 
           break; 



           //lineCounter = 0; 
           //destiNationFile.Flush();         
           //destiNationFile.Dispose(); 
           //destiNationFile = new StreamWriter(string.Format(fileName, fileCounter + 1)); 
           //fileCounter++; 
          } 

          //destiNationFile.WriteLine(bank); 
          //lineCounter++;     
         } 
         //fileCounter++;       
         //} 
        }      
        timer.Stop(); 

        Console.WriteLine(timer.Elapsed.Seconds); 
       } 
       catch (Exception) 
       { 

        throw; 
       } 



      // Keep the console window open in debug mode. 

      System.Console.WriteLine("Press any key to exit."); 
      System.Console.ReadKey(); 
     } 

ответ

1

Попробуйте этот код:
Кроме того, пожалуйста принять к сведению, что для демонстрационных целей я сделал класс BankAcoutNumbers который имеет член BANS

using System; 
using System.Collections.Generic; 
using System.Diagnostics; 
using System.IO; 

namespace ConsoleApplication1 
{ 
    class BankAcoutNumbers 
    { 
     public List<int> BANS { get; set; } 

     public BankAcoutNumbers() 
     { 
      BANS = new List<int>(); 
      BANS.Add(1456456465); 
      BANS.Add(2456465); 
      BANS.Add(342346465); 
      BANS.Add(445645646); 
      BANS.Add(545636546); 
      BANS.Add(64556465); 
      BANS.Add(7456465); 
      BANS.Add(842346465); 
      BANS.Add(9456456); 
      BANS.Add(10456365); 
      BANS.Add(11456456); 
      BANS.Add(12456465); 
      BANS.Add(1342346); 
      BANS.Add(1445645); 
      BANS.Add(1545636); 
      BANS.Add(1645645); 
      BANS.Add(1745646); 
      BANS.Add(1842345); 
      BANS.Add(194564); 
      BANS.Add(2045635); 
      BANS.Add(214564); 
      BANS.Add(224564); 
      BANS.Add(234234); 
      BANS.Add(244564); 
      BANS.Add(254563); 
     } 
    } 


    class Program 
    { 
     static void Main(string[] args) 
     { 

      string path = @"C:\"; 

      //string fileName = string.Format("{0}{1}-", part[0], part[part.Count - 1]); 
      //var bans = BankAcoutNumbers.BANS; 
      const int MAX_FILES = 10; 
      const int BANS_PER_FILE = 10; 
      int bansCounter = 0; 
      var part = new List<int>(); 
      //string fileName = string.Format("{0}-{1}", part[0], part[part.Count - 1]); 

      var maxNumberOfFiles = 10; 
      Stopwatch timer = new Stopwatch(); 



      var fileCounter = 0; 

      if (!Directory.Exists(path)) 
      { 
       DirectoryInfo di = Directory.CreateDirectory(path); 
      } 

      //var destiNationFile = new StreamWriter(string.Format(fileName, fileCounter + 1)); 
      try 
      { 

       // foreach (var bank in BankAcoutNumbers.BANS.Take(100)) 
       //{ 


       while (fileCounter <= maxNumberOfFiles) 
       { 
        timer.Start(); 
        foreach (var bank in new BankAcoutNumbers().BANS) 
        { 
         part.Add(bank); 
         if (++bansCounter >= BANS_PER_FILE) 
         { 
          string fileName = string.Format("{0}-{1}", part[0], part[part.Count - 1]); 
          string outputToFile = ""; 
          //var destinationFile = new StreamWriter(fileName); 
          //destiNationFile = new StreamWriter(fileName); 
          Console.WriteLine("NR{0}", fileName); 
          fileName = @"C:\" + fileName; 
          foreach (var partBan in part) 
          { 
           outputToFile += partBan + Environment.NewLine; 
           Console.WriteLine(partBan); 
          } 
          System.IO.File.WriteAllText(fileName, outputToFile); 
          part.Clear(); 
          bansCounter = 0; 

          if (++fileCounter >= MAX_FILES) 
           break; 


         } 


        } 

       } 
       timer.Stop(); 
       Console.WriteLine(timer.Elapsed.Seconds); 
      } 
      catch (Exception) 
      { 

       throw; 
      } 



      // Keep the console window open in debug mode. 

      System.Console.WriteLine("Press any key to exit."); 
      System.Console.ReadKey(); 
     } 
    } 
} 
+0

Спасибо! Yura –

0

https://msdn.microsoft.com/en-us/library/8bh11f1k.aspx имеет хорошую функцию:

System.IO.File.WriteAllText (@ "C: \ Users \ Public \ TestFolder \ WriteText.txt ", текст);

+0

Благодарим Вас за response.I triied что , Я попытался с // string fileName. Вы видите это в коде. Но это не работает –

+0

Привет, Юра, вы можете посмотреть на угрозу: http://stackoverflow.com/questions/28085999/writinging-directly-to-filesystem. Потому что прямое письмо в файловую систему происходит намного быстрее, но в файле есть только одна строка. Спасибо –

1

Хорошо. Я попробовал это и получил следующие файлы на диске c: 42346465-456456465, 456365465-456456465, 456456465-456365465, 456456465-456465, 456465-42346465. Также внутри файлов был контент, который я отправляю им.

using System; 
using System.Collections.Generic; 
using System.Diagnostics; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace ConsoleApplication1 
{ 
    class BankAcoutNumbers 
    { 
     public List<int> BANS { get; set; } 

     public BankAcoutNumbers() 
     { 
      BANS = new List<int>(); 
      BANS.Add(456456465); 
      BANS.Add(456465); 
      BANS.Add(42346465); 
      BANS.Add(456456465); 
      BANS.Add(456365465); 
     } 
    } 


    class Program 
    { 
     static void Main(string[] args) 
     { 

      string path = @"C:\"; 

      //string fileName = string.Format("{0}{1}-", part[0], part[part.Count - 1]); 
      //var bans = BankAcoutNumbers.BANS; 
      const int MAX_FILES = 10; 
      const int BANS_PER_FILE = 2; 
      int bansCounter = 0; 
      var part = new List<int>(); 
      //string fileName = string.Format("{0}-{1}", part[0], part[part.Count - 1]); 

      var maxNumberOfFiles = 10; 
      Stopwatch timer = new Stopwatch(); 



      var fileCounter = 0; 

      if (!Directory.Exists(path)) 
      { 
       DirectoryInfo di = Directory.CreateDirectory(path); 
      } 

      //var destiNationFile = new StreamWriter(string.Format(fileName, fileCounter + 1)); 
      try 
      { 

       // foreach (var bank in BankAcoutNumbers.BANS.Take(100)) 
       //{ 


       while (fileCounter <= maxNumberOfFiles) 
       { 
        timer.Start(); 
        foreach (var bank in new BankAcoutNumbers().BANS) 
        { 
         part.Add(bank); 
         if (++bansCounter >= BANS_PER_FILE) 
         { 
          string fileName = string.Format("{0}-{1}", part[0], part[part.Count - 1]); 
          //var destinationFile = new StreamWriter(fileName); 
          //destiNationFile = new StreamWriter(fileName); 
          Console.WriteLine("NR{0}", fileName); 
          fileName = @"C:\" + fileName; 
          foreach (var partBan in part) 
          { 
           Console.WriteLine(partBan); 
           System.IO.File.WriteAllText(fileName, partBan.ToString()); 
          } 

          part.Clear(); 
          bansCounter = 0; 

          if (++fileCounter >= MAX_FILES) 
           break; 



          //lineCounter = 0; 
          //destiNationFile.Flush();         
          //destiNationFile.Dispose(); 
          //destiNationFile = new StreamWriter(string.Format(fileName, fileCounter + 1)); 
          //fileCounter++; 
         } 

         //destiNationFile.WriteLine(bank); 
         //lineCounter++;     
        } 
        //fileCounter++;       
        //} 
       } 
       timer.Stop(); 

       Console.WriteLine(timer.Elapsed.Seconds); 
      } 
      catch (Exception) 
      { 

       throw; 
      } 



      // Keep the console window open in debug mode. 

      System.Console.WriteLine("Press any key to exit."); 
      System.Console.ReadKey(); 
     } 
    } 
} 
+0

Оке, спасибо. но теперь в каждом файле просто. Один файл. И в каждом файле должно быть 10 строк. Я обновил свой пост –

+0

Уточняйте свою проблему немного лучше, пожалуйста. Вы упомянули, что вам нужно писать в файл. Функция System.IO.File.WriteAllText будет записывать в файл с именем fileName all, что является строкой partBan. Это зависит от вас, как вы будете делать имя файла, и что вы будете вставлять в partBan. –

+0

записывает одну строку в каждом файле не десять строк. Но в консоли я вижу по десять строк в каждом файле. –

Смежные вопросы