如何将数据从变量保存到文件?
How do I save data from a variable to a file?
所以我有一个方法,我想跟踪它被调用了多少次。
using System;
public class Program
{
public static int useCount;
public static void Main()
{
Add(1, 3);
Add(2, 4);
Console.WriteLine(useCount);
}
public static int Add(int A, int B)
{
int sum = A + B;
useCount++;
return sum;
}
}
有没有办法将 useCount 保存到文件或其他东西?另外,如果我可以将它保存到文件中,它是否可以更新?就像 useCount 是 4,但随后程序重新启动,useCount 是 7,我可以将它添加到显示 11 的保存文件中,而不是覆盖之前的 useCount 4 吗?
希望我足够连贯。
您可以将值写入文件。
在你的程序的顶部写上“using System.IO”来实现这个库。
using System.IO; //implement the library of the file operations.
if(File.Exists("<Here you puth path>"))
{
int value = int.Parse(File.ReadAllText("<Here you put path>")); /* gives the current number value of on the file */
value += useCount; //adds to the current value of the file
File.WriteAllText("<Here you put path>", value.ToString()); //updates the value
}
您可以使用序列化。
https://docs.microsoft.com/en-us/dotnet/standard/serialization/basic-serialization
与其将变量保存到文件,不如将对象的状态保存到文件,这意味着您必须采取 class 方法。
你应该有这样的 class :
[Serializable]
class MyClass
{
private int useCount;
public int UseCount
{
get { return useCount; }
set { useCount = value; }
}
public int Add(int a, int b)
{
useCount++;
return a + b;
}
}
注意开头的[Serializable]注解
现在在您的主程序中,您可以这样做来保存对象的状态:
class Program
{
static void Main(string[] args)
{
var my = new MyClass();
my.Add(5, 5);
my.Add(5, 5);
my.Add(5, 5);
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("MyFile.bin", FileMode.Create, FileAccess.Write, FileShare.None);
formatter.Serialize(stream, my);
stream.Close();
}
}
此程序将创建二进制“MyFile.bin”文件。您可以重命名它...
要恢复对象的状态,您可以在主程序中执行此操作:
class Program
{
static void Main(string[] args)
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("MyFile.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
var obj = (MyClass)formatter.Deserialize(stream);
stream.Close();
// Here's the proof.
Console.WriteLine("useCount: {0}", obj.UseCount);
Console.ReadKey();
}
}
结果将是 3,因为您调用了 Add 方法 3 次...
所以我有一个方法,我想跟踪它被调用了多少次。
using System;
public class Program
{
public static int useCount;
public static void Main()
{
Add(1, 3);
Add(2, 4);
Console.WriteLine(useCount);
}
public static int Add(int A, int B)
{
int sum = A + B;
useCount++;
return sum;
}
}
有没有办法将 useCount 保存到文件或其他东西?另外,如果我可以将它保存到文件中,它是否可以更新?就像 useCount 是 4,但随后程序重新启动,useCount 是 7,我可以将它添加到显示 11 的保存文件中,而不是覆盖之前的 useCount 4 吗?
希望我足够连贯。
您可以将值写入文件。 在你的程序的顶部写上“using System.IO”来实现这个库。
using System.IO; //implement the library of the file operations.
if(File.Exists("<Here you puth path>"))
{
int value = int.Parse(File.ReadAllText("<Here you put path>")); /* gives the current number value of on the file */
value += useCount; //adds to the current value of the file
File.WriteAllText("<Here you put path>", value.ToString()); //updates the value
}
您可以使用序列化。
https://docs.microsoft.com/en-us/dotnet/standard/serialization/basic-serialization
与其将变量保存到文件,不如将对象的状态保存到文件,这意味着您必须采取 class 方法。
你应该有这样的 class :
[Serializable]
class MyClass
{
private int useCount;
public int UseCount
{
get { return useCount; }
set { useCount = value; }
}
public int Add(int a, int b)
{
useCount++;
return a + b;
}
}
注意开头的[Serializable]注解
现在在您的主程序中,您可以这样做来保存对象的状态:
class Program
{
static void Main(string[] args)
{
var my = new MyClass();
my.Add(5, 5);
my.Add(5, 5);
my.Add(5, 5);
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("MyFile.bin", FileMode.Create, FileAccess.Write, FileShare.None);
formatter.Serialize(stream, my);
stream.Close();
}
}
此程序将创建二进制“MyFile.bin”文件。您可以重命名它...
要恢复对象的状态,您可以在主程序中执行此操作:
class Program
{
static void Main(string[] args)
{
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("MyFile.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
var obj = (MyClass)formatter.Deserialize(stream);
stream.Close();
// Here's the proof.
Console.WriteLine("useCount: {0}", obj.UseCount);
Console.ReadKey();
}
}
结果将是 3,因为您调用了 Add 方法 3 次...