(1) Save the source object to TXT text in a certain format
Public bool savetotxtfile (list <pollutionsource> pslist)
{
Bool bresult = false;
System. Io. filestream FS = NULL;
System. Io. streamwriter Sw = NULL;
Try
{
// Create a file stream
FS = new system. Io. filestream (application. startuppath + "/data/info1.txt", system. Io. filemode. Create, system. Io. fileaccess. Write );
// Create the input stream corresponding to the file
Sw = new system. Io. streamwriter (FS );
// Write information to the input stream
Sw. Write (string. Format ("{0} \ t {1} \ t {2} \ t {3} \ t {4} \ r \ n ",
"Name", "type", "place", "date "));
Foreach (pollutionsource PS in pslist)
{
Sw. Write (string. Format ("{0} \ t {1} \ t {2} \ t {3} \ t {4} \ r \ n ",
PS. Name, PS. type, PS. Place, PS. Date ));
}
Bresult = true;
}
Catch
{
Bresult = false;
}
Finally
{
If (SW! = NULL)
{
Sw. Close ();
}
If (FS! = NULL)
{
FS. Close ();
}
}
Return bresult;
}
(2) read TXT text information
Public list <Tuli> readtxtfile (string colorfilename, string gradefilename)
{
List <Tuli> tulis = new list <Tuli> ();
System. Io. filestream FS = NULL;
System. Io. streamreader sr = NULL;
If (system. Io. file. exists (colorfilename) & system. Io. file. exists (gradefilename ))
{
Try
{
If (tulis = NULL)
Tulis = new list <Tuli> ();
// Read the color information
// Using Automatically releases resources
Using (FS = new system. Io. filestream (colorfilename, system. Io. filemode. Open ))
{
Sr = new system. Io. streamreader (FS, system. Text. encoding. Default );
String line = NULL;
System. Drawing. color = new system. Drawing. color ();
String [] RGB = new string [4];
For (line = Sr. Readline (); line! = NULL; line = Sr. Readline ())
{
If (! (Line. Trim () = ""))
{
Tuli = new Tuli ();
RGB = line. Split (New String [] {""}, stringsplitoptions. removeemptyentries );
Tuli. color = system. drawing. color. fromargb (convert. toint32 (RGB [0]), convert. toint32 (RGB [1]), convert. toint32 (RGB [2]);
// Tuli. value = RGB [3];
Tulis. Add (Tuli );
}
}
// Read the classification information
FS = new system. Io. filestream (gradefilename, system. Io. filemode. Open );
Sr = new system. Io. streamreader (FS, system. Text. encoding. Default );
Int I = 0;
String line1 = NULL;
For (line = Sr. Readline (); line! = NULL ;)
{
Line1 = Sr. Readline ();
If (line1! = NULL)
{
Tulis [I]. value = line + "-" + line1;
}
I ++;
Line = line1;
}
}
}
Catch (exception ex)
{
Throw (Ex );
}
Finally
{
If (SR! = NULL)
{
Sr. Dispose ();
Sr. Close ();
}
If (FS! = NULL)
{
FS. Dispose ();
FS. Close ();
}
}
}
Else
{
Tulis = NULL;
}
Return tulis;
}