从WinForms应用程序以.Txt文件存储数据

bez91

我有一个非常基本的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#相当陌生,并且对执行此操作的知识有限。因此,任何建议将不胜感激。

阿鲁·迪内什(Arul Dinesh)

这些示例说明了将文本写入文件的各种方法。前两个示例在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

这里参考

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

安全的地方存储Winforms应用程序的AuthClientSecret?

显示通过Winforms应用程序通过http访问的txt文件

WinForms N 层应用程序,带有 DI、存储库和

当我单击按钮时刷新整个用户在WinForms应用程序中输入的数据

哪些文件需要与cefsharp winforms应用程序一起打包

Winforms 应用程序:如何将 .mdf 保存到 Windows %USERPROFILE% 文件夹?

如何为Winforms桌面应用程序部署可编辑的内容文件?

如何在.NET 4.0 C#WinForms应用程序中播放SWF文件?

独立python应用程序的数据存储

颤动文件存储应用程序状态

查找自己的应用程序存储的文件

节点应用程序和文件存储

Rider 上的 Winforms 应用程序给出错误:“无法加载文件或程序集 'EnvDTE,版本 = 8.0.0.0,文化 = 中性...'

向我的Winforms应用程序添加命令行支持时,使用相同的EXE文件或添加新的控制台应用程序项目更好吗?

将两个表单中的数据保存到Winforms应用程序中的同一表中

连接到本地SQL Server数据库的VB.NET WinForms应用程序的正确连接字符串

如何在C#Winforms中的app.config文件上动态更新应用程序设置键值对

清除应用程序存储

当 UWP 应用程序关闭时,使用来自 TextBox 的数据写入 .txt 文件

如何在 WinForms VB.NET 应用程序中设置 WebView2 用户数据文件夹?

SQLite数据和其他应用程序保存的文件存储在哪里?

安装时将图像存储在应用程序数据文件夹中

用于 SwiftUI 预览的应用程序数据文件存储在哪里

退出应用程序后,文件中存储的数据将被删除

Cordova应用程序在新的应用程序版本中保留本地存储数据吗?

将本机iOS应用程序的数据迁移到React Native应用程序的异步存储

在挂起或关闭应用程序时从TextBox写入.txt文件

在哪里存储ElectronJS应用程序的敏感数据?

为React应用程序存储获取的数据的常用方法?