Hello!

I come here with a tricky question and I do not know how to code it at all.

First off, I am coding in Visual Studio 2010 C# Windows Forms Application

I need someone to help me code the following example:

I have a text file which includes only 3 numbers;

0.625"0.04"1

The textfile is in a different position than the .exe, actually in a folder inside another folder next to the .exe.

I want to read those lines and be able to use them as I will calculate stuff from it, from this example I would use those numbers as:

0.625 / (1 - 0.04) which would give me 0.625 / 0.96 = 0,651xxxxxx

and being able to write that answer in a textbox.

To sum things up..

Read from Text file, giving different value for each number (until hit QUOTE), use the new values to calculate, write out the answer in a textbox.

It is probably much to ask, but it would help me a lot!

I hope you can help me! Ask any question Please ask. Thanks

Edited 3 Years Ago by Xakzi

Use an OpenFileDialog box to have the person select the file.
Use File.ReadAllLines to get what is in the file.
Use String.Split to split the line into it's three parts.
Use Double.TryParse to convert the strings into doubles.
Do your math.
Set Textbox.Text to the result, converted to a string (ToString() being the easiest).

Hi
make file location configuration in your app.config and then write code to read the file.

You can also use openfile dialog as said by Momerath

Edited 3 Years Ago by vinay13mar

Thanks for the answers.. I did try something out but it did not work as I get the error

"An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll

This is the code I tried out.

string textfileopen = "New Folder\\testfile.txt";

            try
            {
                StreamReader DataIn = File.OpenText(textfileopen); // Opening testfile.txt
                string DataStr;
                int count = 0;

                //Test values of double
                double stringtodoubletest; 
                double stringtodoubletestanothernumber;
                double addingbothdoubletogether;
                //Test values of string
                string doubletostringtest;

                string[] result; //Give each split a value of result[x]
                char[] separ = new char[] { '"' }; // split words in text file

                while ((DataStr = DataIn.ReadLine()) != null)
                {
                    count++;
                    result = DataStr.Split(separ);

                    textBox1.Text = result[0]; //write out result to textbox
                    textBox2.Text = result[1];
                    textBox3.Text = result[2];

                    stringtodoubletest = Convert.ToDouble(result[0]); //convert result[0] to double
                    stringtodoubletestanothernumber = Convert.ToDouble(result[1]); //convert result[1] to double

                    //Adding result[0] & result[1] into a test value
                    addingbothdoubletogether = stringtodoubletest + stringtodoubletestanothernumber;

                    //test value (double) to string
                    doubletostringtest = Convert.ToString(addingbothdoubletogether);

                    //write out test string to empty textbox
                    textBox4.Text = doubletostringtest;
                }
            }
            catch (InvalidCastException ex)
            {
                MessageBox.Show("error:" + ex.Message);
            }

You don't say on what line it happened, but I'm guessing it's one of the Convert.ToDouble lines. That's why I said to use Double.TryParse.

Okey so I removed what gave me an error, which was from line:

stringtodoubletest = Convert.ToDouble(result[0]);

down to textbox4 writeout..

and added this code instead;

double.TryParse(result[0], out stringtodoubletest);
double.TryParse(result[1], out stringtodoubletestanothernumber);

addingbothdoubletogether = stringtodoubletest + stringtodoubletestanothernumber;

doubletostringtest = System.Convert.ToString(addingbothdoubletogether);

textBox4.Text = doubletostringtest;

but all I get are 0 in textbox, does not matter what numbers the text file contains.

Edited 3 Years Ago by Xakzi

Figured it out, ended up me using this code.

double.TryParse(result[0], NumberStyles.Any, NumberFormatInfo.CurrentInfo, out result0);
double.TryParse(result[1], NumberStyles.Any, NumberFormatInfo.CurrentInfo, out result1);
double.TryParse(result[2], NumberStyles.Any, NumberFormatInfo.CurrentInfo, out result2);

CalculateResult = result0 / (result2 - result1);

textBox4.Text = CalculateResult.ToString();

Works splendid, thanks to your answer of me using tryparse ^^

Really appriciate it

This question has already been answered. Start a new discussion instead.