Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 保存文本框中的文本_C#_Windows_Forms - Fatal编程技术网

C# 保存文本框中的文本

C# 保存文本框中的文本,c#,windows,forms,C#,Windows,Forms,我有这些文本框,用户在其中输入数据,然后按下按钮来处理数据。现在,用户输入的数据很多,为了给用户一些空闲时间,我想让它成为可能,只要你按下按钮,应用程序就会保存数据,所以当你关闭应用程序并再次启动它时,文本框中会填充上一次输入的数据 我正在考虑使用一个.txt文件来保存数据。只有我在这方面发现了一些困难。其中一个问题是,每次我尝试运行应用程序时,都会从microsoft.NET Framework获取一个messagebox。messagebox表示索引超出了数组的边界。即使我认为我的代码没有超

我有这些文本框,用户在其中输入数据,然后按下按钮来处理数据。现在,用户输入的数据很多,为了给用户一些空闲时间,我想让它成为可能,只要你按下按钮,应用程序就会保存数据,所以当你关闭应用程序并再次启动它时,文本框中会填充上一次输入的数据

我正在考虑使用一个.txt文件来保存数据。只有我在这方面发现了一些困难。其中一个问题是,每次我尝试运行应用程序时,都会从microsoft.NET Framework获取一个messagebox。messagebox表示索引超出了数组的边界。即使我认为我的代码没有超出数组的界限

下面是我使用的代码:

首先,我声明了一个数组,并用包含文本框内容的变量填充它:

string[]settings = new string[5];
settings[0] = openKey;
settings[1] = secretKey;
settings[2] = statusRequestPath;
settings[3] = statusRequestAPI;
settings[4] = setSeconds.ToString();
然后,我使用以下代码将数据写入文本文件

using (StreamWriter writeFile = new StreamWriter(@"C:\Audio Silence Detector\AudioSilenceDetector.txt"))
{
    foreach (string line in settings)
    {
        writeFile.WriteLine(line);
    }
}
为了将.txt文件的文本放回应用程序中,我将其放在formload中:

string[] lines = System.IO.File.ReadAllLines(@"C:\Audio Silence Detector\AudioSilenceDetector.txt");

tbOpenKey.Text = lines[0];
tbSecretKey.Text = lines[1];
tbStatusRequestPath.Text = lines[2];
tbStatusRequestAPI.Text = lines[3];
tbSeconds.Text = lines[4];
我将代码更改为此,它似乎已修复了我遇到的问题:

            if (lines.LongLength == 5)
        {
            tbOpenKey.Text = lines[0];
            tbSecretKey.Text = lines[1];
            tbStatusRequestPath.Text = lines[2];
            tbStatusRequestAPI.Text = lines[3];
            tbSeconds.Text = lines[4];
        }

问题在于文件加载

string[] lines = System.IO.File.ReadAllLines(@"C:\Audio Silence Detector\AudioSilenceDetector.txt");
您无法确定
行现在是否包含5个元素。你可能应该检查一下

if(lines.Length == 5)
{
    tbOpenKey.Text = lines[0];
    tbSecretKey.Text = lines[1];
    tbStatusRequestPath.Text = lines[2];
    tbStatusRequestAPI.Text = lines[3];
    tbSeconds.Text = lines[4];
}
else
{
    MessageBox.Show("Input Data is Wrong");
}

问题在于文件加载

string[] lines = System.IO.File.ReadAllLines(@"C:\Audio Silence Detector\AudioSilenceDetector.txt");
您无法确定
行现在是否包含5个元素。你可能应该检查一下

if(lines.Length == 5)
{
    tbOpenKey.Text = lines[0];
    tbSecretKey.Text = lines[1];
    tbStatusRequestPath.Text = lines[2];
    tbStatusRequestAPI.Text = lines[3];
    tbSeconds.Text = lines[4];
}
else
{
    MessageBox.Show("Input Data is Wrong");
}

问题在于文件加载

string[] lines = System.IO.File.ReadAllLines(@"C:\Audio Silence Detector\AudioSilenceDetector.txt");
您无法确定
行现在是否包含5个元素。你可能应该检查一下

if(lines.Length == 5)
{
    tbOpenKey.Text = lines[0];
    tbSecretKey.Text = lines[1];
    tbStatusRequestPath.Text = lines[2];
    tbStatusRequestAPI.Text = lines[3];
    tbSeconds.Text = lines[4];
}
else
{
    MessageBox.Show("Input Data is Wrong");
}

问题在于文件加载

string[] lines = System.IO.File.ReadAllLines(@"C:\Audio Silence Detector\AudioSilenceDetector.txt");
您无法确定
行现在是否包含5个元素。你可能应该检查一下

if(lines.Length == 5)
{
    tbOpenKey.Text = lines[0];
    tbSecretKey.Text = lines[1];
    tbStatusRequestPath.Text = lines[2];
    tbStatusRequestAPI.Text = lines[3];
    tbSeconds.Text = lines[4];
}
else
{
    MessageBox.Show("Input Data is Wrong");
}

哪一行获得IndexOutOfBounds异常?我怀疑你是第一次打开应用程序,没有文本文件可读取,或者是一个空文件。正如@NicolasTyler提到的,如果(lines!=null&&lines.Length==5)
@Sinatr
lines
不能为null,请在填写文本框之前进行检查。它将是一个数组实例,或者将引发异常。哪一行获得IndexOutOfBounds异常?我怀疑你是第一次打开应用程序,没有文本文件可读取,或者是一个空文件。正如@NicolasTyler提到的,如果(lines!=null&&lines.Length==5)
@Sinatr
lines
不能为null,请在填写文本框之前进行检查。它将是一个数组实例,或者将引发异常。哪一行获得IndexOutOfBounds异常?我怀疑你是第一次打开应用程序,没有文本文件可读取,或者是一个空文件。正如@NicolasTyler提到的,如果(lines!=null&&lines.Length==5)
@Sinatr
lines
不能为null,请在填写文本框之前进行检查。它将是一个数组实例,或者将引发异常。哪一行获得IndexOutOfBounds异常?我怀疑你是第一次打开应用程序,没有文本文件可读取,或者是一个空文件。正如@NicolasTyler提到的,如果(lines!=null&&lines.Length==5)
@Sinatr
lines
不能为null,请在填写文本框之前进行检查。它将是一个数组实例,或者将引发异常。谢谢。这似乎解决了我的问题。谢谢。这似乎解决了我的问题。谢谢。这似乎解决了我的问题。谢谢。这似乎解决了我的问题。