C#读取生成文件
public class Class1 {
/// <summary>
/// 根据字符串Mat生成i行
/// </summary>
/// <param name="sMat">每行文本</param>
/// <param name="iRes">iRes行</param>
/// <returns></returns>
public static string CreateTxt(string sMat,int iRes)
{
StringBuilder sb = new StringBuilder();
//int iRes = 14;
//string sMat = "XCPP-1816";
for (int i = 0; i < iRes; i++)
{
sb.Append(sMat+ Environment.NewLine);
}
return sb.ToString();
}
/// <summary>
/// 根据Mat的行数生成重复iMat行的Res文本
/// </summary>
/// <param name="iMat"></param>
public static void CreateRESTxt(int iMat)
{
StringBuilder sb = new StringBuilder();
//int iRes = 14;
//string sMat = "XCPP-1816";
string sRes = System.IO.File.ReadAllText(@"E:vs2015_projectODBCtestConTestODBCinDebugRes15Text.txt");
for (int i = 0; i <= iMat; i++)
{
sb.Append(sRes + Environment.NewLine);
}
Save_txt(sb.ToString(),"Create15Res");
}
// 24mTimeText.txt
/// <summary>
///
/// </summary>
/// <param name="txt"></param>
/// <param name="names"></param>
public static void Save_txt(string txt, string names)
{
string t = DateTime.Now.ToString("MM-dd");//获取当天时间 并以”-“来定义格式yyyy-
string path = t + names + ".txt";//用当天时间 定义文件名
if (File.Exists(path))//判断路径是否存在
{
Console.WriteLine("ok");
}
else
{
File.Create(path).Dispose();//不存在就创建文件名 并且为了防止文件占用 给他dispose
}
StreamWriter sw = File.AppendText(path); //添加数据
sw.Write(txt);//写入文件
sw.Flush();
sw.Close();//关闭
}
public static string[] ReadTime()
{
string text = System.IO.File.ReadAllText(@"E:vs2015_projectODBCtestConTestODBCinDebug24mTime15Text.txt");
string[] arrayMat = text.Split(new string[] { "
" }, StringSplitOptions.None);
return arrayMat;
}
public static string[] ReadMat()
{
string text = System.IO.File.ReadAllText(@"E:vs2015_projectODBCtestConTestODBCinDebugMat15Text.txt");
string[] arrayMat = text.Split(new string[] { "
" }, StringSplitOptions.None);
return arrayMat;
}
}
Program.cs
string sMat = ""; string stxt = "";
//string[] arrayTime = Class1.ReadTime();
//for (int i = 0; i < arrayTime.Length; i++)
//{
// sMat = (Convert.ToDouble(arrayTime[i]) * 60).ToString();
// stxt = Class1.CreateTxt(sMat.Trim(),12);
// Class1.Save_txt(stxt, "Create15Time");
//}
//string[] arrayMat = Class1.ReadMat();
//for (int i = 0; i < arrayMat.Length; i++)
//{
// sMat = arrayMat[i];//"XCPP-1816"
// stxt = Class1.CreateTxt(sMat.Trim(),12);
// Class1.Save_txt(stxt, "Create15MAT");
//}
Class1.CreateRESTxt(23);
Console.WriteLine("sucess!");
Console.ReadKey();
以上是 C#读取生成文件 的全部内容, 来源链接: utcz.com/z/510885.html