0

I have a very basic C# WinForms application to generate random numbers. The code is shown below:

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();
}

Now what I want do be able to do is on the button click save the random number that is generated in a locally saved file, along with a timestamp.

I am fairly new to C# and have a limited knowledge on how to do this. Therefore any suggestions would be highly appreciated.

4

5 回答 5

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);

有关更多详细信息和示例,请参阅文档

编辑: 我的缺少时间戳,但是这里有很多有价值的答案可以添加它:)

于 2013-11-05T10:40:25.400 回答
0

要将文本保存到文件中,您需要使用 IO 命名空间:

System.IO.File.AppendAllText(@"C:\Test.txt", txbResult && DateTime.Now.ToString());

这些东西向您展示了如何将字符串值写入文件。

编辑:添加了时间戳值。

于 2013-11-05T10:40:49.370 回答
0

这些示例显示了将文本写入文件的各种方法。前两个示例使用 System.IO.File 类上的静态方法将完整的字符串数组或完整的字符串写入文本文件。Example #3 展示了当你必须在写入文件之前单独处理每一行时如何将文本添加到文件中。示例 1-3 都覆盖了文件中的所有现有内容。Example #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

这里参考

于 2013-11-05T10:42:37.030 回答
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。

于 2013-11-05T10:42:47.470 回答
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

于 2013-11-05T10:43:53.663 回答