08-11-2012, 16:54
Hello,
I'm coding a small plugin for stats, and i got problems with reading/writing file.
I made 2 functions, 1 for reading the file, and 1 to write it.
Function to read:
Function to write:
The function to read the stats file is called on "OnServerLoad".
The function to write the stats file is called on "OnPreMapChange".
The write function throw an exception that says "The file is already used by an other processus...".
If i take off the function to read, the write function work well.
So it seems li8ke the read function doesn't close the stats file stream... but in the code i close it so i don't understand what append.
I hope someone will be able to help me with this
Thx in advance and sorry again for my english.
I'm coding a small plugin for stats, and i got problems with reading/writing file.
I made 2 functions, 1 for reading the file, and 1 to write it.
Function to read:
CSHARP Code
- private void ReadServerStats()
- {
- try
- {
- // Create a StreamReader to read the file
- // Get the first line
- string line = monStreamReader.ReadLine();
- // Loop var needed lool <img src="https://www.itsmods.com/forum/images/smilies/wink.gif" alt="Wink" title="Wink" class="smilie smilie_2" />
- int loop = 0;
- // Read all liunes, line per line
- while (line != null)
- {
- // If this is not a comment line
- if (!line.StartsWith("//"))
- {
- string[] infos = line.Split('=');
- if (infos.Length == 2)
- {
- switch (infos[0])
- {
- case "UpTime":
- ServerStats[0] = Int32.Parse(infos[1]);
- break;
- case "TotalKills":
- ServerStats[1] = Int32.Parse(infos[1]);
- break;
- case "MapsPlayed":
- ServerStats[2] = Int32.Parse(infos[1]);
- break;
- case "TotalConnections":
- ServerStats[3] = Int32.Parse(infos[3]);
- break;
- default:
- ServerPrint("Plugin zKokosStats: Unknown config option found '" + infos[0] + "'");
- break;
- }
- ServerStats[loop] = Int32.Parse(infos[1]);
- }
- }
- line = monStreamReader.ReadLine();
- loop += 1;
- }
- // Close the stream
- monStreamReader.Close();
- }
- catch (Exception ex)
- {
- // Exception
- ServerPrint("Fucking exception:"+ex.Message);
- }
- }
Function to write:
CSHARP Code
- private void WriteServerStats()
- {
- // create a writer and open the file
- try
- {
- ServerPrint("StatswillbeWritten2");
- // write the serverstats contents
- tw.WriteLine("UpTime=" + ServerStats[0]);
- tw.WriteLine("TotalKills=" + ServerStats[1]);
- tw.WriteLine("MapsPlayed=" + ServerStats[2]);
- tw.WriteLine("TotalConnections=" + ServerStats[3]);
-
- // close the stream
- tw.Close();
- }
- catch (Exception ex)
- {
- // Exception
- ServerPrint(ex.Message);
- }
- }
The function to read the stats file is called on "OnServerLoad".
The function to write the stats file is called on "OnPreMapChange".
The write function throw an exception that says "The file is already used by an other processus...".
If i take off the function to read, the write function work well.
So it seems li8ke the read function doesn't close the stats file stream... but in the code i close it so i don't understand what append.
I hope someone will be able to help me with this
Thx in advance and sorry again for my english.