
-----------------------------------
ProgrammingFun
Fri Feb 24, 2012 8:57 pm

[C#] Reading and Writing from Text File
-----------------------------------
Hello,

I am completely lost in C# as I have never used it before. However, I am trying to add a feature in the following code where custom variables can be imported from a text file (and if possible, exported, but that's not as important. Can someone please guide me on how I can accomplish this?

Thanks

Code is as follows and is also available at http://dl.dropbox.com/u/34501712/CSettingChangeOnMap.cs :

using System;
using System.IO;
using System.Text;
using System.Threading;
using System.Reflection;
using System.Collections.Generic;
using System.Data;
using System.Text.RegularExpressions;

using PRoCon.Core;
using PRoCon.Core.Plugin;
using PRoCon.Core.Plugin.Commands;
using PRoCon.Core.Players;
using PRoCon.Core.Players.Items;
using PRoCon.Core.Battlemap;
using PRoCon.Core.Maps;

namespace PRoConEvents
{
  public class CSettingChangeOnMap : PRoConPluginAPI, IPRoConPluginInterface
  {
    private int m_mapIndex;
    private int m_nextIndex;
    private List myMapList;
    private List gameMode;
    private List numRounds;
    private List variableNames;
    private List customVariables;
    private List defaultVariables;
    private List defaultVariablesBackup;

    private bool cameFromRunNext;
    private bool needsMapInitialization;
    private bool needsModeChange;
    private List variableHasBeenSet;
    private enumBoolYesNo refreshMapList;

    private void setNormalMode()
    {
      // Normal Preset Variables
      this.ExecuteCommand("procon.protected.send", "vars.autoBalance", "true");
      this.ExecuteCommand("procon.protected.send", "vars.friendlyFire", "false");
      this.ExecuteCommand("procon.protected.send", "vars.killCam", "true");
      this.ExecuteCommand("procon.protected.send", "vars.miniMap", "true");
      this.ExecuteCommand("procon.protected.send", "vars.hud", "true");
      this.ExecuteCommand("procon.protected.send", "vars.3dSpotting", "true");
      this.ExecuteCommand("procon.protected.send", "vars.nameTag", "true");
      this.ExecuteCommand("procon.protected.send", "vars.3pCam", "true");
      this.ExecuteCommand("procon.protected.send", "vars.regenerateHealth", "true");
      this.ExecuteCommand("procon.protected.send", "vars.vehicleSpawnAllowed", "true");
      this.ExecuteCommand("procon.protected.send", "vars.soldierHealth", "100");
      this.ExecuteCommand("procon.protected.send", "vars.playerRespawnTime", "100");
      this.ExecuteCommand("procon.protected.send", "vars.onlySquadLeaderSpawn", "false");

      // Remaining Variables Set From User's Default Variable List
      for (int i = 13; i < defaultVariables.Count; i++)
      {
        if (defaultVariables

-----------------------------------
z_cross_fire
Sat Feb 25, 2012 12:53 am

Re: [C#] Reading and Writing from Text File
-----------------------------------
This might help:


/* Program that reads console input, writes it to file;
 * and reprints it after reading it from file.
*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO; // Need to import this

namespace Files
{
    class Program
    {
        static void Main(string

-----------------------------------
ProgrammingFun
Sat Feb 25, 2012 6:19 am

RE:[C#] Reading and Writing from Text File
-----------------------------------
Thanks, what would I do with this program?
Would it run at the same time that the other plugin is running?
Actually, this looks more like BufferedReader so I'll try to integrate it into the plugin and see if I can do it.

EDIT: I think I understand how I can use this, however, how can I check that the text file exists before initializing this read?

-----------------------------------
mirhagk
Sat Feb 25, 2012 9:17 am

RE:[C#] Reading and Writing from Text File
-----------------------------------
File.Exists() Also you could use a try catch block or a using statement and simply open the file and try to use it (if it fails you'll jump out of the loop to the catch block, or the end of the using statement, depending on which you use).

-----------------------------------
ProgrammingFun
Sat Feb 25, 2012 12:27 pm

RE:[C#] Reading and Writing from Text File
-----------------------------------
Thanks, how would I get something like the following to work?


        String eatmapsetting;
        StreamReader sr = new StreamReader("csconfig_" + whichmap.ToString() + ".txt"); 
        eatmapsetting = (sr.ReadLine());
        this.ExecuteCommand("procon.protected.send", "vars.autoBalance", eatmapsetting);


Also, if I was to use readline again, would it automatically read from the next line?
VStudio also says that the last line in the above code is an error while it is being used in the setNormalMode() method without problems (above). Is this just random or is it actually an error?

-----------------------------------
Vaughan H.
Mon Feb 27, 2012 2:29 pm

Re: [C#] Reading and Writing from Text File
-----------------------------------
If you're creating configuration files, I highly reccomend you making a Configuration class and look into serialization. It'll automatically write the members / properties of the class (your variable options) to a file (binary or XML) and provide an automated mechanism for loading them all back, too.
