110,536
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 读取文本文件内容
/// </summary>
/// <param name="filePath">文件路径绝对路径,如 C:\1.txt</param>
/// <returns></returns>
public static string ReadTextFile(string filePath)
{
string strFileContent = "";
if (File.Exists(filePath))
{
strFileContent = File.ReadAllText(filePath);
}
return strFileContent;
}
/// <summary>
/// 将文件内容存入filePath指定文件,替换原有内容,若文件不存在,则新建文件
/// </summary>
/// <param name="filePath">文件绝对路径</param>
/// <param name="content">文件内容</param>
/// <returns></returns>
public static bool ModifyFile(string filePath, string content)
{
if (File.Exists(filePath))
{
File.Delete(filePath);
}
using (FileStream fs = File.Create(filePath))
{
Byte[] file = new UTF8Encoding(true).GetBytes(content);
fs.Write(file, 0, file.Length);
}
return File.Exists(filePath);
}
string source =
"309019,fhn4guxnj3baupp401o5awfg,/search/?keyword=名品打折网,,2012-07-02 10:16:38.580,2012/7/2,GET,0,61.49.0.229,Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; GTB6.4; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; CIBA; SE 2.X MetaSr 1.0)";
string Pattern = "keyword=([^,]+)";
var result = Regex.Match(source, Pattern).ToString().Replace("keyword=","");