标签:
以下是我使用反射的形式打印到文本里。
string fname = Directory.GetCurrentDirectory() + "\\LogFile.log";
FileInfo finfo = new FileInfo(fname);
if (!finfo.Exists)
{
FileStream fs;
fs = File.Create(fname);
fs.Close();
finfo = new FileInfo(fname);
}
if (finfo.Length > 1024 * 1024 * 10)
{
/**/
///文件超过10MB则重命名
File.Move(Directory.GetCurrentDirectory() + "\\LogFile.log", Directory.GetCurrentDirectory() + DateTime.Now.TimeOfDay + "\\LogFile.log");
/**/
///删除该文件
//finfo.Delete();
}
using (FileStream fs = finfo.OpenWrite())
{
/**/
///根据上面创建的文件流创建写数据流
StreamWriter w = new StreamWriter(fs);
w.BaseStream.Seek(0, SeekOrigin.End);
w.Write("{0} {1} \n\r", DateTime.Now.ToLongDateString(), DateTime.Now.ToLongTimeString());
var sele = db.NikeDTCDailyReport.Where(p => true).ToList();
//创建一个Ilist Dictionary键对值的形式存储
IList<Dictionary<string, object>> id = new List<Dictionary<string, object>>();
//循环sele集合里的值
foreach (var item in sele)
{
//获取item的Type实列 搜索GetProperties当前Type属性 Instance指定实列成员 public指定公共属性
//DeclaredOnly只考虑在所提供类型的层次结构上声明的成员不考虑继承成员
PropertyInfo[] prop = item.GetType().GetProperties(BindingFlags.Instance|BindingFlags.Public|BindingFlags.DeclaredOnly);
//把prop的 循环放入values里
Dictionary<string, object> values = new Dictionary<string, object>();
foreach (PropertyInfo preper in prop)
{
values.Add(preper.Name, preper.GetValue(item, preper.GetIndexParameters()));
}
id.Add(values);
}
//打印Dictionary里的valuezhi值
foreach (var item in id)
{
foreach (var item1 in item)
{
w.WriteLine(item1.Value);
}
w.WriteLine("\n----------------------------");
}
w.Flush();
w.Close();
}
标签:
原文地址:http://www.cnblogs.com/xiaojian1/p/5470617.html