C#在文件中的certin位置添加文本 [英] C# add text in a certin place in file

查看:81
本文介绍了C#在文件中的certin位置添加文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧我已创建代码来搜索文件,但我想在游戏脚本文件的函数中添加文本这是我已经完成的代码。



ok i have created code to search though a file but i want to add text in a function in a game script file this is the code i have done.

public void insidemain()
{
    // this is for all functions inside of main and there are a few so we start here
    string[] lines = null;
    try
    {

        lines = File.ReadAllLines(elfenliedprogsettings.Read("Mod") + @"\scripts\zm\" + elfenliedprogsettings.Read("ModName") + ".csc");

    }
    catch (Exception ex)
    {
        XtraMessageBox.Show(ex.Message, "ERROR adding values to mapname.csc ERROR CODE 002");
    }

    if (lines != null)
    {


        using (StreamReader elfenlied = new StreamReader((elfenliedprogsettings.Read("Mod") + @"\scripts\zm\" + elfenliedprogsettings.Read("ModName") + ".csc")))
        {
            string elfenliedtopfan5_program = elfenlied.ReadToEnd();
            if (elfenliedtopfan5_program.Contains("#precache( \"client_fx\", SNOW_FX );"))
            {
                //MessageBox.Show("SCAR-H ALREADY ADDED.");
                _MessageResult.Instance.Append("Already Added :  devine calls to : " + elfenliedprogsettings.Read("Mod") + @"\scripts\zm\" + elfenliedprogsettings.Read("ModName") + ".csc" + Environment.NewLine);
            }
            if (!elfenliedtopfan5_program.Contains("#precache( \"client_fx\", SNOW_FX );"))
            {
                elfenlied.Dispose();
                if (File.Exists(elfenliedprogsettings.Read("Mod") + @"\scripts\zm\" + elfenliedprogsettings.Read("ModName") + ".csc"))
                {
                    {


                        string s =
                                   "#define SNOW_FX \"dlc0/factory/fx_snow_player_os_factory\"\r\n"
                                  + "#precache( \"client_fx\", SNOW_FX );\r\n";


                        string file = elfenliedprogsettings.Read("Mod") + @"\scripts\zm\" + elfenliedprogsettings.Read("ModName") + ".csc";
                        List<string> elf = new List<string>(System.IO.File.ReadAllLines(file));
                        int index = elf.FindIndex(item => item.Contains("function main()" + "{"));
                        if (index != -1)
                        {
                            elf.Insert(index + 1, s);//""
                        }
                        System.IO.File.WriteAllLines(file, elf);
                        //richTextBoxEx1.LoadFile(Properties.Settings.Default.mods + "//mod.csv", RichTextBoxStreamType.PlainText);
                        //MessageBox.Show("Added: " + s + " Mods.csv");
                        _MessageResult.Instance.Append("\r\nAdded:  " + s + elfenliedprogsettings.Read("Mod") + @"\scripts\zm\" + elfenliedprogsettings.Read("ModName") + ".csc" + Environment.NewLine);

                    }
                }
            }

        }
    }

}





但我有问题的部分我无法将文本放入括号内我需要将文本放在main函数内



但我不知道如何判断这一点。



文件



but the part im having issues with i can not get the text to go inside the brackets i need the text to be placed inside the mainfunction

but i i not sure how to accumplish this.

the file

#using scripts\codescripts\struct;
#using scripts\shared\audio_shared;
#using scripts\shared\callbacks_shared;
#using scripts\shared\clientfield_shared;
#using scripts\shared\exploder_shared;
#using scripts\shared\scene_shared;
#using scripts\shared\util_shared;

#insert scripts\shared\shared.gsh;
#insert scripts\shared\version.gsh;

#using scripts\zm\_load;
#using scripts\zm\_zm_weapons;

//Perks
#using scripts\zm\_zm_pack_a_punch;
#using scripts\zm\_zm_perk_additionalprimaryweapon;
#using scripts\zm\_zm_perk_doubletap2;
#using scripts\zm\_zm_perk_deadshot;
#using scripts\zm\_zm_perk_juggernaut;
#using scripts\zm\_zm_perk_quick_revive;
#using scripts\zm\_zm_perk_sleight_of_hand;
#using scripts\zm\_zm_perk_staminup;

//Powerups
#using scripts\zm\_zm_powerup_double_points;
#using scripts\zm\_zm_powerup_carpenter;
#using scripts\zm\_zm_powerup_fire_sale;
#using scripts\zm\_zm_powerup_free_perk;
#using scripts\zm\_zm_powerup_full_ammo;
#using scripts\zm\_zm_powerup_insta_kill;
#using scripts\zm\_zm_powerup_nuke;

//Traps
#using scripts\zm\_zm_trap_electric;

#using scripts\zm\zm_usermap;



function main()
{
	zm_usermap::main();

	include_weapons();
	
	util::waitforclient( 0 );
}


function include_weapons()
{
	zm_weapons::load_weapon_spec_from_table("gamedata/weapons/zm/zm_levelcommon_weapons.csv", 1);
}





也不能这样做,因为用户将拥有主要功能之上的功能所以总结我需要看看主要功能找到然后找到}起始支架并在那里添加文字



提前谢谢你elfenliedtopfan5



我尝试了什么:



在这个问题上做了很多谷歌搜索,并做了上面描述的事情。



also cant just do } as the user will have functions above the main function so in summary i need to look for the main function find that then find the } starting brace and add the text in there

thank you in advance elfenliedtopfan5

What I have tried:

did a lot of google searching on the issue plus doing what is discribed above.

推荐答案

您好,您可以在文件中添加注释行,例如:

Hi, you can add a comment line in your file, for example:
//{newline}



现在,无论何时你想在文件中添加内容都只需要替换:


now, whenever you want to add something to the file just do a replace:

myfile.Replace ("//{newline}", "newinstrution \n //{newline}");



所以你总是会有一把钥匙用新指令替换。


so you'll always have a key to replace with a new instruction.


这是我要做的事情



找到
This is what i would do

after you find
function main




var text = string.join("\n", elf);
int index = text.indexOf("function main");
if (index != -1){
int foundedIndex =-1;
while(index < elf.Count() -1 && foundIndex == -1){
index++;
char str = text [index];
if (str == '{'){
foundedIndex = index +1;
}
if (foundIndex)
text = text.Insert(foundedIndex, "your text after the bracket")


这篇关于C#在文件中的certin位置添加文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆