将表单项属性保存到文本文件

时间:2013-12-02 04:46:18

标签: c# arrays

我是一个初学者,试图编写一个方法来将表单项属性保存到文本文件中。我认为我的循环存在一些问题。

public void SaveFormItems(object sender, List<IFormItem> Items,string fileName)
  {        
        string fn = path+fileName+".txt";
        string[] content = null; 
        string[] line = null;
        int count = Items.Count; ;
        for (int i = 1; i < count; i++)
        {
            foreach (var ctrl in Items)
            {
                content[0] = ctrl.Id.ToString();
                content[1] = ctrl.Type.GetTypeCode().ToString();
                content[2] = ctrl.X.ToString();
                content[3] = ctrl.Y.ToString();
                content[4] = ctrl.Height.ToString();
                content[5] = ctrl.Width.ToString();
                content[6] = ctrl.Text.ToString();

                line[i] = String.Join("/t", content);
            }



            if (!File.Exists(fn))
            {
                File.WriteAllLines(path, line, Encoding.UTF8);
            }
        } 

    } 

输出文本文件必须在单独的行中包含每个项属性

FormName
Text    1   500 100 30  200 0   User ID
TextInput   2   700 100 30  200 0   UsesrId
Text    3   500 150 30  200 0   First Name
TextInput   4   700 150 30  200 0   FirstName

1 个答案:

答案 0 :(得分:0)

我希望这会对你有所帮助。

public void SaveFormItems(object sender, List<IFormItem> Items,string fileName)
  {        
        string fn = path+fileName+".txt";
        string[] content = new string[7]; 
        string[] line = null;
        foreach (var ctrl in Items)
            {
                content[0] = ctrl.Id.ToString();
                content[1] = ctrl.Type.GetTypeCode().ToString();
                content[2] = ctrl.X.ToString();
                content[3] = ctrl.Y.ToString();
                content[4] = ctrl.Height.ToString();
                content[5] = ctrl.Width.ToString();
                content[6] = ctrl.Text.ToString();

                line[i] = String.Join("/t", content);

                if (!File.Exists(fn))
                   {
                      File.WriteAllLines(path, line, Encoding.UTF8);
                   }
            }
  }