我有一个非常基本的C#WinForms应用程序来生成随机数。代码如下所示:
private static double RandomNumber(double min, double max)
{
Random random = new Random();
var next = random.NextDouble();
return min +(next * (max - min));
}
private void btnGenerate_Click(object sender, EventArgs e)
{
var maxNum = Convert.ToDouble(txbInput.Text);
var randomDec = Math.Round(RandomNumber(0, maxNum), 2);
txbResult.Text = randomDec.ToString();
}
现在我想做的就是点击按钮,保存在本地保存的文件中生成的随机数,以及时间戳。
我是C#的新手,对如何做到这一点知之甚少。因此,任何建议都将受到高度赞赏。
答案 0 :(得分:0)
来自MSDN的明智之词:
// Example #2: Write one string to a text file.
string text = "A class is the most powerful data type in C#. Like a structure, " +
"a class defines the data and behavior of the data type. ";
// WriteAllText creates a file, writes the specified string to the file,
// and then closes the file.
System.IO.File.WriteAllText(@"C:\Users\Public\TestFolder\WriteText.txt", text);
有关详细信息和示例,请参阅documentation。
修改强> 我错过了时间戳,但是这里有很多值得回答的补充:)
答案 1 :(得分:0)
要将文本保存到文件,您需要使用IO命名空间:
System.IO.File.AppendAllText(@"C:\Test.txt", txbResult && DateTime.Now.ToString());
这个东西向您展示了如何将字符串值写入文件。
编辑:添加了时间戳值。
答案 2 :(得分:0)
这些示例显示了将文本写入文件的各种方法。前两个示例使用System.IO.File类上的静态方法将完整的字符串数组或完整的字符串写入文本文件。示例#3显示了在写入文件之前必须单独处理每一行时如何向文件添加文本。示例1-3全部覆盖文件中的所有现有内容。示例#4显示了如何将文本附加到现有文件。
class WriteTextFile
{
static void Main()
{
// These examples assume a "C:\Users\Public\TestFolder" folder on your machine.
// You can modify the path if necessary.
// Example #1: Write an array of strings to a file.
// Create a string array that consists of three lines.
string[] lines = { "First line", "Second line", "Third line" };
// WriteAllLines creates a file, writes a collection of strings to the file,
// and then closes the file.
System.IO.File.WriteAllLines(@"C:\Users\Public\TestFolder\WriteLines.txt", lines);
// Example #2: Write one string to a text file.
string text = "A class is the most powerful data type in C#. Like a structure, " +
"a class defines the data and behavior of the data type. ";
// WriteAllText creates a file, writes the specified string to the file,
// and then closes the file.
System.IO.File.WriteAllText(@"C:\Users\Public\TestFolder\WriteText.txt", text);
// Example #3: Write only some strings in an array to a file.
// The using statement automatically closes the stream and calls
// IDisposable.Dispose on the stream object.
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Public\TestFolder\WriteLines2.txt"))
{
foreach (string line in lines)
{
// If the line doesn't contain the word 'Second', write the line to the file.
if (!line.Contains("Second"))
{
file.WriteLine(line);
}
}
}
// Example #4: Append new text to an existing file.
// The using statement automatically closes the stream and calls
// IDisposable.Dispose on the stream object.
using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\Users\Public\TestFolder\WriteLines2.txt", true))
{
file.WriteLine("Fourth line");
}
}
}
//Output (to WriteLines.txt):
// First line
// Second line
// Third line
//Output (to WriteText.txt):
// A class is the most powerful data type in C#. Like a structure, a class defines the data and behavior of the data type.
//Output to WriteLines2.txt after Example #3:
// First line
// Third line
//Output to WriteLines2.txt after Example #4:
// First line
// Third line
// Fourth line
来自here
的参考资料答案 3 :(得分:0)
private void WriteData(double value)
{
using (var file = new System.IO.StreamWriter(@"C:\file.txt", true))
{
file.WriteLine(string.Format("{0} {1}", value, DateTime.Now));
}
}
您可以看到此链接msdn。得到时间 - DateTime.Now。
答案 4 :(得分:0)
添加:
// using System.IO;
string filepath = @"C:\test.txt"; //sample file name & location
using (StreamWriter writer = new StreamWriter(filepath))
{
writer.WriteLine(DateTime.Now.ToString() + " " + randomDec.ToString());
} // write your text in a string