5 Replies - 719 Views - Last Post: 21 June 2010 - 02:40 AM Rate Topic: -----

#1 smartflower2002   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 116
  • Joined: 20-May 10

logical help, writing changes to file

Posted 20 June 2010 - 12:23 PM

hello guys,
well, i am working on a project where i have to open a file, read the settings, then display the settings and allow it to be edited, and then after editing save the changed settings to the file
.

for all the first part then editing is done, but i am not able to get how to write the changes only to the file,,.

eg, if a file is
vale a "1"
value b "2"
value c "3"



and users changes the value of b from 2 to 4, then save that changes only.

i am not able to figure out that logical part..

and now my code.. this is how my code works
namespace WindowsFormsApplication5
{
    public partial class Form1 : Form
    {
        string file;
        string[] line;
        string[] GetA;
        string[] GetB;
        string[] GetC;
        string[] GetD;
        int aValue = 0;
        int bValue = 0;
        int cValue = 0;
        int dValue = 0;
        int i = 0;
        int j;
        int k;
        int l;
        int m;
        public Form1()
        {
            InitializeComponent();


            using (StreamReader sr = new StreamReader("c:\\test.cfg"))
            {
                file = sr.ReadToEnd();
            }
            line = file.Split('\n');
           for(i = 0; i < line.Length; i++)
           {
               j = line[i].IndexOf("value a");
               if (j != -1)
               {
                  aValue = i;
               }

               k = line[i].IndexOf("value b");
               if (k != -1)
               {
                   bValue = i;
               }

               l = line[i].IndexOf("value c");
               if (l != -1)
               {
                   cValue = i;
               }

               m = line[i].IndexOf("value d");
               if (m != -1)
               {
                   dValue = i;
               }
           }
           
          
            GetA = line[aValue].Split('"');
            GetB = line[bValue].Split('"');
            GetC = line[cValue].Split('"');
            GetD = line[dValue].Split('"');
            comboBox1.Text = GetA[1];
            comboBox2.Text = GetB[1];
            trackBar1.Value = Convert.ToInt32(GetC[1]);
            label8.Text = GetC[1];

            if(GetD[1] == "1")
            {
                checkBox1.Checked = true;
            }
            else
            {
                checkBox1.Checked = false;
            }
            


        }

        

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void trackBar1_Scroll(object sender, EventArgs e)
        {
            label8.Text = trackBar1.Value.ToString();
            GetC[1] = trackBar1.Value.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            // this button will save the changes
			
        }
    }
}


hope someone can help me, coz i am stuck at the very end part

Is This A Good Question/Topic? 0
  • +

Replies To: logical help, writing changes to file

#2 elbielefeld   User is offline

  • D.I.C Head

Reputation: 70
  • View blog
  • Posts: 217
  • Joined: 18-May 10

Re: logical help, writing changes to file

Posted 20 June 2010 - 12:26 PM

You don't need any logic. Just overwrite the old file with the new values.
Was This Post Helpful? 0
  • +
  • -

#3 smartflower2002   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 116
  • Joined: 20-May 10

Re: logical help, writing changes to file

Posted 20 June 2010 - 12:27 PM

but how to get new values back in the string file?

or consider i created new string write;
then how to replace those edited values in it so new settings will be written?
Was This Post Helpful? 0
  • +
  • -

#4 elbielefeld   User is offline

  • D.I.C Head

Reputation: 70
  • View blog
  • Posts: 217
  • Joined: 18-May 10

Re: logical help, writing changes to file

Posted 20 June 2010 - 12:39 PM

To overwrite a file use this:

            using (FileStream fs = new FileStream("C:\\test.cfg", FileMode.Create)) //Always overwrite
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    sw.WriteLine("value a \"" + ValueA + "\"");
                    //...

                    sw.Close();
                }
            }


Was This Post Helpful? 0
  • +
  • -

#5 smartflower2002   User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 116
  • Joined: 20-May 10

Re: logical help, writing changes to file

Posted 20 June 2010 - 01:21 PM

elbielefeld,
bro u are awesome, ur code gave me a weird idea and it worked like charme

what i did is, i took the lines which i had splited.

then collected the lines which i was going to edit in strings (coz the real file is almost of 400 lines)


then i replaced the new changes Lines, with the old one in string file..

and written the string f to the same file.

and it works like charm.


guys i have one more query.
incase if the code doesnt find the requred file, i will catch that into exception and open a dialog box where user can find the required file manually..

now guys i have seeen at many places when a specific file is not found, a dialog box is opened where by default open file name is set, so user cannot select any other file..

how can we do that?
Was This Post Helpful? 0
  • +
  • -

#6 Adkins   User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: logical help, writing changes to file

Posted 21 June 2010 - 02:40 AM

openFileDialog.Filter = "test.txt|test.txt"


With that little snippet you will only be able to see test.txt in the file dialog. of course you will have to change the openFileDialog to be the name of the dialog you are using. Hope this helped out a bit.
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1