3

How to store and restore a 3D string array to a file in C#? Hi; I am programing a project that I create a class for public Arrays so I can use them in all forms.

class MainArray

{

   // Introduce Moarefi Projeh

   public static string[] MPArray = new string[15];

   // Introduce Moarefi Personel Rozmozd

   public static string[,] MPRArray = new string[20,15];

   // Introduce Moarefi Peymankaran

   public static string[,] MPeymankaranArray = new string[20, 15];

   // Introduce Gozaresh PersonelRozMozd

   public static string[,] GPRMArray = new string[20, 15];

   
   public static string[,,] MArr = new string[20,20,20];

 

}

I Save all Data in these arrays in some forms. I want to create a class that operator can store all these arrays in a file at the end of use the program and restore these data at the beginning of next use the program.

You know this is first my programing in C# please give me a class code that I can do this. I want a class code that I use a simple cod in my main program like this: MyData.WriteArray(MyArrayName,FileName); for store and MyData.ReadArray(MyArrayName,FileName); for restore.

Community
  • 1
  • 1
  • 1
    What you want to do is called [Serialization](http://msdn.microsoft.com/en-us/library/vstudio/ms233843.aspx). What should be format of saved file? Xml or binary? – semao Jan 12 '13 at 10:21

2 Answers2

2

This code will save object of type MyData to binary file:

using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

[Serializable()]
public class MyData
{
     private string[,,] SArr;

     public MyData(int sizex,int sizey,int sizez)
     {
        SArr = new string[sizex,sizey,sizez];
     }         
     public MyData(string[,,] data)
     {
        SArr = data;
     }         
     public string this[x,y,z]
     {
        get
        {
            return SArr[x,y,z];
        }
        set
        {
            SArr[x,y,z] = value;
        }
     }
     public void SaveToFile(String fileName)
     {
          Stream TestFileStream = File.Create(fileName);
          BinaryFormatter serializer = new BinaryFormatter();
          serializer.Serialize(TestFileStream, this);
          TestFileStream.Close();
     }

    public static MyData ReadFromFile(String fileName)
    {
        if (File.Exists(FileName))
        {
            Stream TestFileStream = File.OpenRead(FileName);
            BinaryFormatter deserializer = new BinaryFormatter();
            var data = (MyData)deserializer.Deserialize(TestFileStream);
            TestFileStream.Close();
            return data;
        }
        return null;
    }
}

In code:

var data = new MyData(4,4,4);
MyData[1,1,0] = "TestData";
data.SaveToFile(@"C:\Test.data");
var data2 = MyData.ReadFromFile(@"C:\Test.data");
//data2[1,1,0] == "TestData"
semao
  • 1,757
  • 12
  • 12
  • Hi thanks for your help. this is exactly the cod I want. but I cant use it. where can I define and set my Data to SArr and send it to this Class. And how can I catch my data for example finally I want to use this cod: txtbox1=SArr[1,1,1]; ? – Mehdi Meymari Jan 12 '13 at 13:23
  • I updated the answer to include what you need. But the most simple way would be to change SArr modifier to public (then you would access it like data.SArr = new string[4,4,4]). – semao Jan 12 '13 at 13:56
  • @MehdiMeymari don't forget to accept Semao's answer if it solves your problem – Antonio Bakula Jan 12 '13 at 14:03
1

Before saving you need to serialize the array. Here is a link that might help you out.

Community
  • 1
  • 1
Niar
  • 532
  • 2
  • 11
  • 23