Skip to content

Instantly share code, notes, and snippets.

@smkplus
Created November 20, 2023 08:31
Show Gist options
  • Save smkplus/552ad9672bf362daa961cac1a0840918 to your computer and use it in GitHub Desktop.
Save smkplus/552ad9672bf362daa961cac1a0840918 to your computer and use it in GitHub Desktop.
using System;
using System.IO;
public class Program
{
public static void Main()
{
int counter = ReadCounterFromFile(); // Reading the counter value from a file
// Incrementing the counter
counter = IncrementCounter(counter);
Console.WriteLine("Counter value: " + counter);
// Writing the counter value back to the file
WriteCounterToFile(counter);
System.Windows.Forms.MessageBox.Show(counter.ToString()); // Displaying counter value in a message box
}
// Function to increment the counter
public static int IncrementCounter(int value)
{
return value + 1; // Incrementing the value by 1
}
// Function to read the counter value from a file
public static int ReadCounterFromFile()
{
string filePath = "counter.txt";
int counter = 0;
if (File.Exists(filePath))
{
string storedValue = File.ReadAllText(filePath);
int.TryParse(storedValue, out counter);
}
return counter;
}
// Function to write the counter value to a file
public static void WriteCounterToFile(int value)
{
string filePath = "counter.txt";
File.WriteAllText(filePath, value.ToString());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment