问题描述
好吧,我有一个gsc代码,所有内容都这样添加了
Ok i have a gsc code and everything is added in like this
{
"Type" : "ALIAS",
"Name" : "user_aliases",
"Filename" : "user_aliases.csv",
"Specs" : [ ]
},
我希望能够在文件中查找该代码块,并在}下,我希望能够放置我的新代码,但不确定是否有人可以阐明如何执行此操作对我来说,我将不胜感激
i want to be able to look in the file and look for this block of code and under the }, i want to be able to put my new code but not sure on how to do this if anyone could shed some light on it for me i would be very much appreciated
推荐答案
感谢您在此处发布.
为您写一个小箱子.
下图是两个文件.一个gscfile.gsc(将被插入到文件中)和一个newStr.txt(这是我要插入的内容.);
文档的原始内容(在gscfile.gsc中)
文档内容(在 newStr.txt )
Contents of the document (innewStr.txt)
插入后(在gscfile.gsc中):
这是代码演示
protected void Page_Load(object sender, EventArgs e)
{
string str = GetFileStr(@"gscfile.gsc");
string newstr = GetFileStr(@"newStr.txt");
int num = 2;// Specifies that you want to insert rows in which object
int point = 0;
while (num-->0)
{
point = str.IndexOf( '}' ,++point); // Record index
}
str= str.Insert(point, newstr);
ToStreamWrite(@"gscfile.gsc", str); //To Insert
}
//Rewrite
public void ToStreamWrite(string path,string sText)
{
using (System.IO.StreamWriter sw = new System.IO.StreamWriter(GetPath(path), false))
{
sw.Write(sText);
sw.Flush();
sw.Close();
}
}
//Get the relative path
public string GetPath(string path)
{
return Server.MapPath(path);
}
//Get the contents of the file
public string GetFileStr(string path)
{
string str = "";
using (StreamReader sr = new StreamReader(GetPath(path)))
{
sr.BaseStream.Seek(0, SeekOrigin.Begin);
str = sr.ReadToEnd();
}
return str;
}
此外,根据您提供的gsc文件,我认为它是JSON文件.如果是,我
建议您使用JSON序列化对象来处理此文件 请参阅 json将新对象添加到现有json文件C#
In addition, based on your gsc file that you provided, I guess it is a JSON file. If yes,I
suggest you using JSON Serialization object to treat this file Please see json add new object to existing json file C#
最诚挚的问候,
克里斯汀
这篇关于搜索gsc文件并在某些区域添加代码行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!