How to auto-increment a variable, everytime you run a program? How to make it store its previous value, instead of getting initialized to zero?
Writing test result in a file with line/row no. (File.AppendText)
Once test application is opened, 10 tests run, 0-9 results, Test app. closed.
Next time when you again open test app and run some tests, line no. should start from 10 and not from 0. (Like Index property sql server)
How to achieve this in C#?
Thanks.
3 replies to this topic
#1
Posted 17 December 2010 - 05:43 PM
|
|
|
#2
Posted 19 December 2010 - 06:24 AM
You could write it to a file, but you need to read it back from the file too.
so, you're workflow would have to look something like this.
something possibly like this? (I purposely used a text encoding, although I personally think storing binary is a better solution)
so, you're workflow would have to look something like this.
- Start Application
- If File exists, open it, read the value (store it in a numeric type) and then close the file
- Increment the value
- open the file (if it doesnt exist create it), store the value of the increment
something possibly like this? (I purposely used a text encoding, although I personally think storing binary is a better solution)
public static void Main (string[] args)
{
byte[] buffer;
string file_path = @"/home/mike/testfilestore";
FileInfo fi = new FileInfo(file_path);
int value = 0;
if (fi.Exists) {
using (FileStream fs = fi.OpenRead()) {
try {
buffer = new byte[fi.Length];
fs.Read(buffer, 0, buffer.Length);
value = Convert.ToInt32(Encoding.UTF8.GetString(buffer, 0, buffer.Length));
} catch (Exception) {
Console.WriteLine("Unable to extract value from file...");
}
}
}
Console.WriteLine(++value);
using (FileStream fs = fi.OpenWrite()) {
buffer = Encoding.UTF8.GetBytes(value.ToString());
fs.Write(buffer, 0, buffer.Length);
}
Console.WriteLine ("Press <Enter> to terminate...");
Console.Read();
}
Edited by sam_coder, 19 December 2010 - 06:24 AM.
fixing formatting of code
#3
Posted 19 December 2010 - 06:51 AM
and if you really wanted to store the values as binary, btw, you could do that pretty easily
This would be considered more efficient, in my opinion
I chose to use plain text in the previous example, because the file is human readable.
This would be considered more efficient, in my opinion
I chose to use plain text in the previous example, because the file is human readable.
public static void Main (string[] args)
{
byte[] buffer;
string file_path = @"/home/mike/testfilestore";
FileInfo fi = new FileInfo(file_path);
int value = 0;
if (fi.Exists) {
using (FileStream fs = fi.OpenRead()) {
try {
buffer = new byte[fi.Length];
fs.Read(buffer, 0, buffer.Length);
value = BitConverter.ToInt32(buffer, 0);
} catch (Exception) {
Console.WriteLine("Unable to extract value from file...");
}
}
}
Console.WriteLine(++value);
using (FileStream fs = fi.OpenWrite()) {
buffer = BitConverter.GetBytes(value);
fs.Write(buffer, 0, buffer.Length);
}
Console.WriteLine ("Press <Enter> to terminate...");
Console.Read();
}
#4
Posted 22 December 2010 - 07:09 AM
anyone else hear an echo?:c-whistle:
Edited by sam_coder, 26 December 2010 - 07:29 AM.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









