Register for your free account! | Forgot your password?

Go Back   elitepvpers > MMORPGs > Conquer Online 2 > CO2 Programming
You last visited: Today at 00:44

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



Wrapper for program file initialization

Discussion on Wrapper for program file initialization within the CO2 Programming forum part of the Conquer Online 2 category.

Reply
 
Old   #1
 
elite*gold: 0
Join Date: Jan 2007
Posts: 118
Received Thanks: 20
Wrapper for program file initialization

Just getting bored while studying on C# and make this wrapper for parameter initialization.(Wow C# is amazing).

This will include writing/reading on the parameter initialization. This is useful when you are going to make a program on which you are going to save the parameters in the ini files.

Example:
Copath=D\games\conquer.exe
Myname=xmen01235
....
....
....

Code:
internal class iniParam
    {
        private Dictionary <string,string> _params = new Dictionary<string,string>();
        private string _filename;
        public iniParam(string filename)
        {
            _filename = filename;
            this.Read();
        }
        public void Set(string paramname, string paramvalue)
        {

            if (!_params.ContainsKey(paramname))
                _params.Add(paramname, paramvalue);
            else
            {
                _params.Remove(paramname);
                _params.Add(paramname, paramvalue);
            }                    

        }
        public void Save()
        {
            if (_params.Count == 0)
                return;

            System.IO.StreamWriter oWrite= File.CreateText(this._filename);
            oWrite.Close();
            StreamWriter objStreamWriter = new StreamWriter(this._filename, true, Encoding.Unicode);
            foreach (KeyValuePair<string, string> kvp in _params)
            {
                objStreamWriter.WriteLine(kvp.Key + "=" + kvp.Value);
            }            
            objStreamWriter.Close();
        }
        public void Get(string paramname,out string paramvalue)
        {   
            if (_params.Count == 0)
                paramvalue="";
            if (!_params.TryGetValue(paramname, out  paramvalue))
                paramvalue = "";
        }
        private void Read()
        {
            bool invalidFile = false;
            if(this._filename=="")
            {
                invalidFile = true;
                throw new System.ArgumentException("Invalid Filename!");                
            }
            if (invalidFile)
                return;
            try
            {
                StreamReader reader=new StreamReader(this._filename); 
                while(reader.Peek()>=0)
                {
                    try
                    {
                        string line= reader.ReadLine();
                        string[] strSplit;
                        char _operator='=';
                        strSplit=line.Split(_operator);
                        _params.Add(strSplit[0], strSplit[1]);
                    }                   
                    catch
                    {
                        throw new System.ArgumentException("Error in reading file!"); 
                    }

                }
                reader.Close();
            }
            catch
            {
                System.IO.StreamWriter oWrite=File.CreateText(this._filename);
                oWrite.Close();
            }

        }

    }
Implementation:
Definition
Code:
private iniParam myParam=new iniParam("parameters.ini");
Setting a value
Code:
myParam.Set("Copath", "D:\\games\\conquer.exe");
myParam.Set("Myname", "xmen01235");
myParam.Save();
Reading a value
Code:
string _copath,_myName;
myParam.Get("Copath",out _copath);
myParam.Set("Myname",out _myName);
xmen01235 is offline  
Reply


Similar Threads Similar Threads
[ERROR] ( undefined file pleasez réinstall the program )
10/09/2011 - Kal Online - 4 Replies
Hello guys . i have 3 problems with connecting on a PServer , i'm out of idea so i post here , its my last chance .. Ive download an PServer . Extracted it , updated it . when i start the game , it work , 2 mins after login/playing , my game crash , it close totally without any error . When i want restart it , it say this error like 10 time every second
no codes working when i add them to any file as program.cs
01/27/2011 - CO2 Private Server - 12 Replies
hey guys when i add any code to any file as program.cs it's don't work how i make it work ? or i should do something after i add any code !!!
[QUESTION] What program will open JScript Script File
05/18/2010 - CO2 Private Server - 1 Replies
Hey i was wondering What program will open JScript Script File? please i need answers ASAP !!!!
Can anybody help put CONQUER.EXE PATCH 5219 and ITEMTYPE.DAT 5215 into program file
03/05/2010 - CO2 Programming - 1 Replies
So I'm a noob at this and I don't know where to put these downloads into the program files. I know how to get there but I don't know what to delete and replace with the new hacked files...please help. my email is [email protected].
Undefined file(s) installed. Please reinstall the program.
02/08/2010 - Kal Online - 3 Replies
As Title . P-Server "Fusion" geht nich. Wenn ich Starten will, kommt diese Meldung: "Undefined file(s) installed. Please reinstall the program"



All times are GMT +1. The time now is 00:44.


Powered by vBulletin®
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2026 elitepvpers All Rights Reserved.