Friday, 25 March 2016

C# fileio lecture

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace file3demo
{   public partial class Form1 : Form
    {   public Form1()
        {    InitializeComponent();
        }
        private void READit_Click(object sender, EventArgs e)
        {//read setting
            Settings1 x = new Settings1();
            person.Text = x.person;
            phone.Text = x.phone.ToString();
        }
        private void button1_Click(object sender, EventArgs e)
        {//save setting
            Settings1 x = new Settings1();
            x.person = person.Text;
            x.phone = Convert.ToInt64(phone.Text);
            x.Save();
        }
        private void button3_Click(object sender, EventArgs e)
        {//save file array
            string[] data = { person.Text, phone.Text };
            File.WriteAllLines(@"c:\randall\file.txt", data);
        }
        private void button2_Click(object sender, EventArgs e)
        {//read file array
            openFileDialog1.Filter = "Text|*.txt";
            openFileDialog1.ShowDialog();
            string[] data 
                = File.ReadAllLines(openFileDialog1.FileName);
            person.Text = data[0];
            phone.Text = data[1];
        }

        private void button5_Click(object sender, EventArgs e)
        {//save rtf
            string data = person.Text + "," + phone.Text;
            richTextBox1.Text = data;
            richTextBox1.SaveFile(@"c:\randall\file2.rtf");
        }

        private void button4_Click(object sender, EventArgs e)
        {//read rtf
            richTextBox1.LoadFile(@"c:\randall\file2.rtf");
            string[] data = richTextBox1.Text.Split(',');
            person.Text = data[0];
            phone.Text = data[1];
        }

        private void button7_Click(object sender, EventArgs e)
        {//save with real files
            //old   open "c:\randall\file3.txt" as #1
            //      write #1, person.text,phone.txt
            //      close #1
            FileStream f = new FileStream(@"c:\randall\file3.txt",
                FileMode.Create, FileAccess.Write);
            StreamWriter w = new StreamWriter(f);
            string data = person.Text + "," + phone.Text;
            w.Write(data);
            w.Close();
            f.Close();
        }

        private void button6_Click(object sender, EventArgs e)
        {//read real file
            //old   open "c:\randall\file3.txt" as #1
            //      read #1,person.text,phone.text
            //      close #1
            FileStream f = new FileStream(@"c:\randall\file3.txt",
                FileMode.Open, FileAccess.Read);
            StreamReader r = new StreamReader(f);
            string data = r.ReadLine();//gives null if no data left
            string[] data2 = data.Split(',');
            person.Text = data2[0];
            phone.Text = data2[1];
            r.Close();
            f.Close();
        }

        private void button9_Click(object sender, EventArgs e)
        {//save fixed format
            FileStream f = new FileStream(@"c:\randall\file4.txt",
                FileMode.Create, FileAccess.Write);
            StreamWriter w = new StreamWriter(f);
            //names get a negative size
            //amounts and numbers get a zero fill with a convert
            string data = string.Format("{0,-30}{1,10:0000000000}",
                                person.Text, Convert.ToInt64(phone.Text));
            w.Write(data);
            w.Close();
            f.Close();
        }
        private void button8_Click(object sender, EventArgs e)
        {//read fixed format
            FileStream f = new FileStream(@"c:\randall\file4.txt",
                FileMode.Open, FileAccess.Read);
            StreamReader r = new StreamReader(f);
            string data = r.ReadLine();//gives null if no data left
            //use substring to pull out fixed formats
            person.Text = data.Substring(0, 30);
            phone.Text = data.Substring(30, 10);
            r.Close();
            f.Close();
        }
    }
}

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

namespace file2demo
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] dir = Directory.GetDirectories(@"c:\");
            foreach (string name in dir)
                Console.WriteLine(name);
            string[] files = Directory.GetFiles(@"c:\randall");
            foreach (string name in files)
                Console.WriteLine(name);
            Console.Read();
        }
    }
}
----------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;

namespace file1demo
{   public partial class Form1 : Form
    {   SortedList<int, worker> db = new SortedList<int, worker>();
        public Form1()
        {  InitializeComponent();
        }
        private void button2_Click(object sender, EventArgs e)
        {//add
            worker w = new worker();
            w.person = person.Text;
            w.phone = Convert.ToInt64(phone.Text);
            int key = Convert.ToInt32(id.Text);
            db[key] = w;
            message.Text = "record added";
        }

        private void button3_Click(object sender, EventArgs e)
        {//change
            worker w = new worker();
            w.person = person.Text;
            w.phone = Convert.ToInt64(phone.Text);
            int key = Convert.ToInt32(id.Text);
            db[key] = w;
            message.Text = "record changed";
        }
        private void button4_Click(object sender, EventArgs e)
        {//delete
            int key = Convert.ToInt32(id.Text);
            db.Remove(key);
            message.Text = "record deleted";
            id.Text = "";
            person.Text = "";
            phone.Text = "";
        }
        private void button1_Click(object sender, EventArgs e)
        {//read
            int key = Convert.ToInt32(id.Text);
            if (db.ContainsKey(key))
            {   person.Text = db[key].person;
                phone.Text = db[key].phone.ToString();
                message.Text = "";
            }
            else
            {   person.Text = "";
                phone.Text = "";
                message.Text = "record not found";
            }
        }
    }
    public class worker
    {   public string person;
        public long phone;
    }
}

No comments:

Post a Comment