写入文件系统而不是控制台

时间:2015-01-21 10:55:57

标签: c#-5.0

我想将所有银行编号的编号写入实际文件。不是控制台版本。但我没有看到文件系统中的实际文件。如何将数字写入文件系统?

我有这个:

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();
        }

3 个答案:

答案 0 :(得分: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();
        }
    }
}

答案 1 :(得分: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();
        }
    }
}

答案 2 :(得分:0)

https://msdn.microsoft.com/en-us/library/8bh11f1k.aspx功能很好:

System.IO.File.WriteAllText(@“C:\ Users \ Public \ TestFolder \ WriteText.txt”,text);