File Operations and Launching External Applications

 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.IO;

using System.Diagnostics;

 

namespace XLS

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

           

            if (openFileDialog1.ShowDialog() != DialogResult.OK)

                return;

           

            FileStream file1 = new FileStream(openFileDialog1.FileName,FileMode.Open);

            StreamReader sr = new StreamReader(file1);

            string s1 = sr.ReadToEnd();

 

            string[] s2 = s1.Split('\n');

 

            FileStream []file2 = new FileStream[297];

            StreamWriter []sw = new StreamWriter[297];

 

            for (int i = 1; i < 297; i++)

            {

                string nm = Convert.ToString(i);

                if (i<100) nm = "0"+nm;

                if (i<10) nm = "0"+nm;

                nm = "D:\\Competition3\\" + nm + ".txt";

 

                file2[i] = new FileStream(nm, FileMode.Create);

                sw[i] = new StreamWriter(file2[i]);

                sw[i].WriteLine(s2[0]);

            }

 

            for (int k = 1; k < 9637; k++)

            {

                string[] s3 = s2[k].Split(new char[] {','},StringSplitOptions.RemoveEmptyEntries);

                if (s3.Length < 10)

                    continue;

 

                int i = Convert.ToInt32(s3[0]);

                sw[i].WriteLine(s2[k]);

                if (k % 100 == 0)

                {

                    button1.Text = Convert.ToString(k);

                    button1.Refresh();

                }

            }

 

            for (int i = 1; i < 297; i++)

            {

                sw[i].Close();

                file2[i].Close();               

            }

        }

 

 

 

        private void button3_Click(object sender, EventArgs e)

        {

 

             if (folderBrowserDialog1.ShowDialog() != DialogResult.OK)

                return;

             DirectoryInfo di = new DirectoryInfo(folderBrowserDialog1.SelectedPath);

             FileInfo []fi = di.GetFiles("*.txt");

      

             foreach (FileInfo f in fi)

                  listBox1.Items.Add(f);

        }

 

 

        private void button2_Click(object sender, EventArgs e)

        {

            Process pr = new Process();

            pr.StartInfo.FileName = "Notepad.exe";

            pr.StartInfo.Arguments = "test.txt";

            pr.Start();

        }

    }

}