0

乱数を生成するための非常に基本的な 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#にかなり慣れていないため、これを行う方法についての知識が限られています。したがって、どんな提案でも大歓迎です。

4

5 に答える 5

0

これらの例は、テキストをファイルに書き込むさまざまな方法を示しています。最初の 2 つの例では、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

ここから参照

于 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 に答える
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 に答える