Folls

Hi,

I'm trying to use Visual C# 2005 Express Edition to do the following. Can you please point me in the direction of examples that may help me understand Of course, this is a simplified description. However, I have never done any of this before so any examples will help.

I want to develop two Forms applications.
First Application: It has textboxes and a button.
The user enters numbers in the boxes and clicks the button.
The program does computations, clears the file, and writes values to a file.

Second Application: It also has textboxes and a button.
The user clicks the button, the program reads from the file, does computations,
clears the file, and displays some numbers.

Thank you!

Folls


Re: Visual C# Express Edition Two applications that read and write numbers to a file

VS_Programmer

Try this:

Application 1

button1_Click(object sender, EventArgs e)

{

int num = Convert.ToInt32(textBox1.Text);

textBox1.Clear();

int computations = num;

//do more computations here

File.WriteAllText("C:/data.txt", computations.ToString());

}

Application 2

button1_Click(object sender, EventArgs e)

{

int value = Convert.ToInt32(File.ReadAllText);

int computations = value;

//do more computations here

File.WriteAllText("C:/data.txt", "");

textBox1.Text = computations.ToString();

}

Hope this helps





Re: Visual C# Express Edition Two applications that read and write numbers to a file

Folls

Thank you! That does help considerably. Is there an example anywhere of reading multiple numbers (from a file) that are separated by a space or a comma

For example, is there a method that can read a 5 numbers separated by spaces and put them in an array of int

Thank you!

Folls




Re: Visual C# Express Edition Two applications that read and write numbers to a file

Folls

I have another question also. I am attempting to implement the code above. The following statement is included:

string path = "c:\\ReadWrite.txt";
File.WriteAllText(path, num.ToString());

When I try to run in debug mode, I am getting an "UnauthorizedAccessException" I tried changing the path name. I also tried creating that file from another application and was able to do it. Why wouldn't I be able to create the file

Thanks,

Folls




Re: Visual C# Express Edition Two applications that read and write numbers to a file

VS_Programmer

Make sure you have rights to access the file (administrator, ect.)

Hope this helps





Re: Visual C# Express Edition Two applications that read and write numbers to a file

Folls

Thanks again. I was unable to resolve the file access issue (so far). However, I removed the path and let visual studio put it in the default location and that worked.

I found some parsing info here using split:
http://msdn2.microsoft.com/en-us/library/ms228388(VS.80).aspx

I merged some simplified code into one example for posting. I put both buttons in one form application. This appears to work now. Thanks for your help!

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

//added by me

using System.IO;



namespace ReadWrite2

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}


//path for file to use. @allows paths without escapes

string pathname = @"testreadwrite2.txt";


//write to file button

private void button1_Click(object sender, EventArgs e)

{

//get text from boxes and convert to double

double num1 = Convert.ToDouble(textBox1.Text);

double num2 = Convert.ToDouble(textBox2.Text);


//clear output boxes

textBox3.Clear();

textBox4.Clear();


//do computations

num1 = num1 * 2;

num2 = num2 * 2;


//write to the file. overwrites an existing data.

File.WriteAllText(pathname, num1.ToString() + " " + num2.ToString());

}


//read from file button

private void button2_Click(object sender, EventArgs e)

{

//if the file exists, get the data, change, put in box

if (File.Exists(pathname))

{

//read in the whole line to a string

string fileinput = File.ReadAllText(pathname);


//put each text number in an element of an array

char[] delimiterChars = { ' ' };

string[] numbers = fileinput.Split(delimiterChars);


//convert text to double

double value1 = Convert.ToDouble(numbers[0]);

double value2 = Convert.ToDouble(numbers[1]);


//do computations

value1 = value1 * 2;

value2 = value2 * 2;


//clear file and text boxes

File.WriteAllText(pathname, "");

textBox1.Clear();

textBox2.Clear();


//output values to the text boxes

textBox3.Text = value1.ToString();

textBox4.Text = value2.ToString();


}

}

}

}


Thanks again!

Folls