Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to handle huge strings in C#

Tags:

c#

I have to write the data below to a textfile after replacing two values with ##IP##, ##PORT##. What is the best way? Should I hold all in a string and use Replace and write to textfile?

Data:

[APP]
    iVersion=   101
    pcVersion=  1.01a
    pcBuildDate=Mar 27 2009
[MAIN]
    iFirstSetup=        0
    rcMain.rcLeft=    676
    rcMain.rcTop=     378
    rcMain.rcRight=  1004
    rcMain.rcBottom=  672
    iShowLog=           0
    iMode=              1
[GENERAL]
    iTips=              1
    iTrayAnimation=     1
    iCheckColor=        1
    iPriority=          1
    iSsememcpy=         1
    iAutoOpenRecv=      1
    pcRecvPath=         C:\Documents and Settings\karthikeyan\My Documents\Downloads\fremote101a\FantasyRemote101a\recv
    pcFileName=         FantasyRemote
    iLanguage=          1
[SERVER]
    iAcceptVideo=       1
    iAcceptAudio=       1
    iAcceptInput=       1
    iAutoAccept=        1
    iAutoTray=          0
    iConnectSound=      1
    iEnablePassword=    0
    pcPassword=
    pcPort=          7902
[CLIENT]
    iAutoConnect=       0
    pcPassword=
    pcDefaultPort=   7902
[NETWORK]
    pcConnectAddr=##IP##
    pcPort=##Port##
[VIDEO]
    iEnable=                  1
    pcFcc=                 AMV3
    pcFccServer=           
    pcDiscription=         
    pcDiscriptionServer=   
    iFps=                    30
    iMouse=                   2
    iHalfsize=                0
    iCapturblt=               0
    iShared=                  0
    iSharedTime=              5
    iVsync=                   1
    iCodecSendState=          1
    iCompress=                2
    pcPlugin=              
    iPluginScan=              0
    iPluginAspectW=          16
    iPluginAspectH=           9
    iPluginMouse=             1
    iActiveClient=            0
    iDesktop1=                1
    iDesktop2=                2
    iDesktop3=                0
    iDesktop4=                3
    iScan=                    1
    iFixW=                   16
    iFixH=                    8
[AUDIO]
    iEnable=                  1
    iFps=                    30
    iVolume=                  6
    iRecDevice=               0
    iPlayDevice=              0
    pcSamplesPerSec=    44100Hz
    pcChannels=      2ch:Stereo
    pcBitsPerSample=      16bit
    iRecBuffNum=            150
    iPlayBuffNum=             4
[INPUT]
    iEnable=                  1
    iFps=                    30
    iMoe=                     0
    iAtlTab=                  1
[MENU]
    iAlwaysOnTop=             0
    iWindowMode=              0
    iFrameSize=               4
    iSnap=                    1
[HOTKEY]
    iEnable=                            1
    key_IDM_HELP=              0x00000070
    mod_IDM_HELP=              0x00000000
    key_IDM_ALWAYSONTOP=       0x00000071
    mod_IDM_ALWAYSONTOP=       0x00000000
    key_IDM_CONNECT=           0x00000072
    mod_IDM_CONNECT=           0x00000000
    key_IDM_DISCONNECT=        0x00000073
    mod_IDM_DISCONNECT=        0x00000000
    key_IDM_CONFIG=            0x00000000
    mod_IDM_CONFIG=            0x00000000
    key_IDM_CODEC_SELECT=      0x00000000
    mod_IDM_CODEC_SELECT=      0x00000000
    key_IDM_CODEC_CONFIG=      0x00000000
    mod_IDM_CODEC_CONFIG=      0x00000000
    key_IDM_SIZE_50=           0x00000074
    mod_IDM_SIZE_50=           0x00000000
    key_IDM_SIZE_100=          0x00000075
    mod_IDM_SIZE_100=          0x00000000
    key_IDM_SIZE_200=          0x00000076
    mod_IDM_SIZE_200=          0x00000000
    key_IDM_SIZE_300=          0x00000000
    mod_IDM_SIZE_300=          0x00000000
    key_IDM_SIZE_400=          0x00000000
    mod_IDM_SIZE_400=          0x00000000
    key_IDM_CAPTUREWINDOW=     0x00000077
    mod_IDM_CAPTUREWINDOW=     0x00000004
    key_IDM_REGION=            0x00000077
    mod_IDM_REGION=            0x00000000
    key_IDM_DESKTOP1=          0x00000078
    mod_IDM_DESKTOP1=          0x00000000
    key_IDM_ACTIVE_MENU=       0x00000079
    mod_IDM_ACTIVE_MENU=       0x00000000
    key_IDM_PLUGIN=            0x0000007A
    mod_IDM_PLUGIN=            0x00000000
    key_IDM_PLUGIN_SCAN=       0x00000000
    mod_IDM_PLUGIN_SCAN=       0x00000000
    key_IDM_DESKTOP2=          0x00000078
    mod_IDM_DESKTOP2=          0x00000004
    key_IDM_DESKTOP3=          0x00000079
    mod_IDM_DESKTOP3=          0x00000004
    key_IDM_DESKTOP4=          0x0000007A
    mod_IDM_DESKTOP4=          0x00000004
    key_IDM_WINDOW_NORMAL=     0x0000000D
    mod_IDM_WINDOW_NORMAL=     0x00000004
    key_IDM_WINDOW_NOFRAME=    0x0000000D
    mod_IDM_WINDOW_NOFRAME=    0x00000002
    key_IDM_WINDOW_FULLSCREEN= 0x0000000D
    mod_IDM_WINDOW_FULLSCREEN= 0x00000001
    key_IDM_MINIMIZE=          0x00000000
    mod_IDM_MINIMIZE=          0x00000000
    key_IDM_MAXIMIZE=          0x00000000
    mod_IDM_MAXIMIZE=          0x00000000
    key_IDM_REC_START=         0x00000000
    mod_IDM_REC_START=         0x00000000
    key_IDM_REC_STOP=          0x00000000
    mod_IDM_REC_STOP=          0x00000000
    key_IDM_SCREENSHOT=        0x0000002C
    mod_IDM_SCREENSHOT=        0x00000002
    key_IDM_AUDIO_MUTE=        0x00000073
    mod_IDM_AUDIO_MUTE=        0x00000004
    key_IDM_AUDIO_VOLUME_DOWN= 0x00000074
    mod_IDM_AUDIO_VOLUME_DOWN= 0x00000004
    key_IDM_AUDIO_VOLUME_UP=   0x00000075
    mod_IDM_AUDIO_VOLUME_UP=   0x00000004
    key_IDM_CTRLALTDEL=        0x00000023
    mod_IDM_CTRLALTDEL=        0x00000003
    key_IDM_QUIT=              0x00000000
    mod_IDM_QUIT=              0x00000000
    key_IDM_MENU=              0x0000007B
    mod_IDM_MENU=              0x00000000
[OVERLAY]
    iIndicator=            1
    iAlphaBlt=             1
    iEnterHide=            0
    pcFont=     MS UI Gothic
[AVI]
    iSound=                    1
    iFileSizeLimit=       100000
    iPool=                     4
    iBuffSize=                32
    iStartDiskSpaceCheck=      1
    iStartDiskSpace=        1000
    iRecDiskSpaceCheck=        1
    iRecDiskSpace=           100
    iCache=                    0
    iAutoOpen=                 1
    pcPath=C:\Documents and Settings\karthikeyan\My Documents\Downloads\fremote101a\FantasyRemote101a\avi
[SCREENSHOT]
    iSound=       1
    iAutoOpen=    1
    pcPath=       C:\Documents and Settings\karthikeyan\My Documents\Downloads\fremote101a\FantasyRemote101a\ss
    pcPlugin=   BMP
[CDLG_SERVER]
    mrcWnd.rcLeft=      667
    mrcWnd.rcTop=       415
    mrcWnd.rcRight=    1013
    mrcWnd.rcBottom=    634
[CWND_CLIENT]
    miShowLog=            0
    m_iOverlayLock=       0
[CDLG_CONFIG]
    mrcWnd.rcLeft=      467
    mrcWnd.rcTop=       247
    mrcWnd.rcRight=    1213
    mrcWnd.rcBottom=    802
    miTabConfigSel=       2
like image 745
Anuya Avatar asked May 31 '10 07:05

Anuya


4 Answers

Why does that look to me like an INI file? Try using Nini.

EDIT: Added sample

Here is the simple sample how you can use Nini library.

    using System;
    using Nini;
    using Nini.Ini;
    using Nini.Config;
    using System.IO;

    namespace CommandLine
    {
        class Test
        {
            static void Main(string[] Args)
            {
                string myIniTxt = Path.Combine(Environment.CurrentDirectory, "ini.txt");
                IniConfigSource ini = new IniConfigSource(myIniTxt);
                IConfig config = ini.Configs["NETWORK"];
                config.Set("pcConnectAddr", "myValueForPcConnectAdd");
                config.Set("pcPort", "myValueForPcPort");
                ini.Save();
            }
        }
    }

Remark: First link for "Nini" seems to do not work well but here is some others.

http://sourceforge.net/projects/nini/

http://nini.sourceforge.net/Manual/NiniManual.htm

like image 91
adopilot Avatar answered Nov 11 '22 04:11

adopilot


I suggest you just read the file and replace. It should be fine. If you're worried about performance I suggest you measure the performance and try to fix later if it's a problem.

Example.

string file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "foo.file");
string s = File.ReadAllText(file);

s = s.Replace("##IP##", "127.0.0.1");
s = s.Replace("##PORT##", "80");

File.WriteAllText(file, s);
like image 37
Carl Bergquist Avatar answered Nov 11 '22 03:11

Carl Bergquist


I think you can use a StringBuilder here.

The StringBuilder lets you manipulate / replace a string in the same memory location.

Every time you manipulate a string, new memory is allocated to hold the result, and old string is garbage collected. The StringBuilder is specially designed to make mutable strings.

Edit after reading comment: The template (the text without the replacements) can be put in a project setting. The IDE will take care of the all the rest.

If it is really huge, it's better to put it in a file in the installation folder. Then you have to make sure the file is installed together with your appliation.

You put the file "MyTemplate.txt" in a StringBuilder like this:

var fileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase )),
    "MyTemplate.txt");
var stringBuilder = new StringBuilder(File.ReadAllText(fileName));
like image 3
GvS Avatar answered Nov 11 '22 04:11

GvS


If you're dead sure about the size of the file and you know it won't exceed say a couple of hundred MB then read it in memory and do a regexp replace. However, for flexibility, I would ready the file one line at a time, make the replacement and write out to a second file.

like image 1
Raj Avatar answered Nov 11 '22 03:11

Raj