加载后无法在c#中保存txt文件

时间:2016-03-18 23:02:02

标签: c# text save project

我会尽量简短。我正在制作一个我的节目  自己的意志,作为视频游戏的实用工具(Dark Souls II。)的一部分  程序的作用是将字符构建保存到文本文件中  也可以加载文本文件并用新信息覆盖它们。它  需要将txt文件拆分为可以在其中修改的数组  加载文件 - 这样可以将文件保存到另一个文件。有很多  代码,所以我只会发布相关的东西,但它给了我  自保存文本的方式以来,在特定位置超出界限  文件很长,我只能用逗号分割。

private void btnChar_Click(object sender, EventArgs e)
  {
            string fullChar = full[0] 
                + full[1]
                + full[2]
                + full[3]
                + full[4]
                + full[5]
                + full[6]
                + full[7]
                + full[8]
                + full[9]
                + full[10]
                + full[11]
                + full[12]
                + full[13]
                + full[14]
                + full[15]
                + full[16]
                +full[17];
            FlexibleMessageBox.Show(fullChar);
        }
 private void saveCharacterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog1 = new SaveFileDialog();
            saveFileDialog1.Filter = "Text Documents (.txt)|*.txt";
            saveFileDialog1.DefaultExt = ".text";
            saveFileDialog1.FilterIndex = 2;
            saveFileDialog1.RestoreDirectory = true;
            string fullChar = full[0] + ", "
                + full[1] + ", "
                + full[2] + ", "
                + full[3] + ", "
                + full[4] + ", "
                + full[5] + ", "
                + full[6] + ", "
                + full[7] + ", "
                + full[8] + ", "
                + full[9] + ", "
                + full[10] + ", "
                + full[11] + ", "
                + full[12] + ", "
                + full[13] + ", "
                + full[14] + ", "
                + full[15] + ", "
                + full[16] + ", "
                + full[17] + ", ";
            //Save file dialog for saving characters
            try
            {
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {           
                    //Code to write the stream goes here.
                    File.WriteAllText(saveFileDialog1.FileName, fullChar);               
                }
            }
           catch (Exception etc)
            {
                MessageBox.Show("An error occured: " + etc.Message);
            }
        }

        private void loadCharacterToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog();
            openFileDialog1.Filter = "Text Documents (.txt)|*.txt";
            openFileDialog1.FilterIndex = 2;
            openFileDialog1.RestoreDirectory = true;
            string fullChar;
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                //Code to write the stream goes here.
                fullChar = openFileDialog1.FileName;
                full = fullChar.Split(',');
            }
        }         

1 个答案:

答案 0 :(得分:1)

如果你不确定整个阵列的有效长度(你说18个元素或更少),那么你不能盲目地假设你拥有所有18个元素。

相反,您可以使用string.Join方法构建要显示或写入文件的字符串

private void btnChar_Click(object sender, EventArgs e)
{
    string fullChar = string.Join(",", full);
    FlexibleMessageBox.Show(fullChar);
}

private void saveCharacterToolStripMenuItem_Click(object sender, EventArgs e)
{
    SaveFileDialog saveFileDialog1 = new SaveFileDialog();
    saveFileDialog1.Filter = "Text Documents (.txt)|*.txt";
    saveFileDialog1.DefaultExt = ".text";
    saveFileDialog1.FilterIndex = 2;
    saveFileDialog1.RestoreDirectory = true;
    string fullChar = string.Join(", ", full);        
    //Save file dialog for saving characters
    ......
}

但错误的真正原因在于应该获取文件内容的代码。在获取文件名后,您需要阅读它 实际代码只是尝试拆分文件名。

if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
    fullChar = File.ReadAllText(openFileDialog1.FileName);
    full = fullChar.Split(',');
}