- 获取文件信息
Directory.Exists(DirectoryPath);
new DirectoryInfo(DirectoryPath);
new FileInfo(FilePath);
- 操作文件夹
DirectoryInfo directoryInfo = Directory.CreateDirectory(LogPath);
Directory.Move(LogPath, LogMovePath);
Directory.Delete(LogMovePath);
- 写入文件
if (!File.Exists(FilePath))
{
using (FileStream fileStream = File.Create(FilePath))
{
string name = "小春的文档"
byte[] bytes = Encoding.Default.GetBytes(name)
fileStream.Write(bytes, 0, bytes.Length)
fileStream.Flush()
}
using (FileStream fileStream = File.Create(FilePath))
{
StreamWriter sw = new StreamWriter(fileStream)
sw.WriteLine("小春的文档")
sw.Flush()
}
using (FileStream sw = File.AppendText(FilePath))
{
string msg = "2023年4月25日10:15:47"
sw.WriteLine(msg)
sw.Flush()
}
using (FileStream sw = File.AppendText(FilePath))
{
string name = "小春的文档 |2023年4月25日10:20:01"
byte[] bytes = Encoding.Default.GetBytes(name)
sw.BaseStream.Write(bytes, 0, bytes.Length)
sw.Flush()
}
}
- 读取文件
foreach (string result in File.ReadAllLine(FilePath))
{
Console.WriteLine(result)
}
string sResult = File.ReadAllText(FilePath)
Byte[] byteContent = File.ReadAllBytes(FilePath)
string sResultByte = System.Text.Encoding.UTF8.GetString(byteContent)
using (FileStream stream = File.OpenRead(FilePath))
{
int length = 5
int result = 0
do
{
byte[] bytes = new byte[length]
result = stream.Read(bytes, 0, 5)
for (int i = 0
{
Console.WriteLine(bytes[i].ToString())
}
} while (length == result)
File.Copy(FilePath, FilePathCopy)
File.Move(FilePath, FilePathMove)
File.Delete(FilePathCopy)
File.Delete(FilePathMove)
}
- 获取驱动器信息
DriverInfo[] drives = DriverInfo.GetDrives();
foreach (DriveInfo drive in drives)
{
if (drive.IsReady)
{
Console.WriteLine("类型:{0} 卷标:{1} 名称:{2} 总空间:{3} 剩余空间:{4}", drive.DriveType, drive.VolumeLabel, drive.Name, drive.TotalSize, drive.TotalFreeSpace);
}
else
{
Console.WriteLine("类型:{0} is not ready", drive.DriveType);
}
Console.WriteLine(Path.GetDirectoryName(LogPath));
Console.WriteLine(Path.GetDirectoryName(@"d:\abc"));
Console.WriteLine(Path.GetDirectoryName(@"d:\abc"));
Console.WriteLine(Path.GetRandomFileName());
Console.WriteLine(Path.GetFileNameWithoutExtension("d:\abc.txt"));
Console.WriteLine(Path.GetInvalidPathChars());
Console.WriteLine(Path.GetInvalidFileNameChars());
Console.WriteLine(Path.Combine(LogPath, "log.txt"));
}
- 日志打印
public static void Log(string msg)
{
StreamWriter sw = null;
try
{
string fileName = "log.txt";
string totalPath = Path.Combine(LogPath, fileName);
if (!Directory.Exists(LogPath))
{
Directory.CreateDirectory(LogPath);
}
sw = File.AppendText(totalPath);
sw.WriteLine(string.Format("{0}:{1}", DateTime.Now, msg));
sw.WriteLine("===================================================");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
if (sw != null)
{
sw.Flush();
sw.Close();
sw.Dispose();
}
}
}
- 系统自带的序列化器使用
public class SerializeHelper
{
public static void BinarySerialize()
{
// 使用二进制序列化对象
string fileName = Path.Combine(Constant.SerializeDataPath, @"BinarySerialize.txt")
using (Stream fStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite))
{
// 需要一个stream,这里是直接写入文件了
List<Programmer> pList = DataFactory.BuildProgrammerList()
BinaryFormatter binFormat = new BinaryFormatter()
binFormat.Serialize(fStream, pList)
}
using (Stream fStream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite))
{
// 需要一个stream,这里是来源于文件
BinaryFormatter binFormat = new BinaryFormatter()
// 使用二进制反序列化对象
fStream.Position = 0
List<Programmer> pList = (List<Programmer>)binFormat.Deserialize(fStream)
}
}
//BinaryFormatter序列化自定义类的对象时,序列化之后的流中带有空字符,以致于无法反序列化,反序列化时总是报错“在分析完成之前就遇到流结尾”(已经调用了stream.Seek(0, SeekOrigin.Begin)
//改用XmlFormatter序列化之后,可见流中没有空字符,从而解决上述问题,但是要求类必须有无参数构造函数,而且各属性必须既能读又能写,即必须同时定义getter和setter,若只定义getter,则反序列化后的得到的各个属性的值都为null。
public static void SoapSerialize()
{
//使用Soap序列化对象
string fileName = Path.Combine(Constant.SerializeDataPath, @"SoapSerialize.txt")
using (Stream fStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite))
{
List<Programmer> pList = DataFactory.BuildProgrammerList()
SoapFormatter soapFormat = new SoapFormatter()
//soapFormat.Serialize(fStream, list)
soapFormat.Serialize(fStream, pList.ToArray())
}
using (Stream fStream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite))
{
SoapFormatter soapFormat = new SoapFormatter()
//使用二进制反序列化对象
fStream.Position = 0
List<Programmer> pList = ((Programmer[])soapFormat.Deserialize(fStream)).ToList()
}
}
public static void XmlSerialize()
{
//使用XML序列化对象
string fileName = Path.Combine(Constant.SerializeDataPath, @"Student.xml")
using (Stream fStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite))
{
List<Programmer> pList = DataFactory.BuildProgrammerList()
XmlSerializer xmlFormat = new XmlSerializer(typeof(List<Programmer>))
xmlFormat.Serialize(fStream, pList)
}
using (Stream fStream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite))
{
XmlSerializer xmlFormat = new XmlSerializer(typeof(List<Programmer>))
//使用XML反序列化对象
fStream.Position = 0
List<Programmer> pList = pList = (List<Programmer>)xmlFormat.Deserialize(fStream)
}
}
public static void Json()
{
List<Programmer> pList = DataFactory.BuildProgrammerList()
string result = JsonHelper.ObjectToString<List<Programmer>>(pList)
List<Programmer> pList1 = JsonHelper.StringToObject<List<Programmer>>(result)
}
}
- 使用序列化器实现实体序列化
public class XmlHelper
{
public static string ToXml<T>(T t) where T : new()
{
Type type = typeof(T);
XmlSerializer xmlSerializer = new XmlSerializer(type);
Stream stream = new MemoryStream();
xmlSerializer.Serialize(stream, t);
stream.Position = 0;
StreamReader reader = new StreamReader(stream);
string text = reader.ReadToEnd();
return text;
}
public static T ToObject<T>(string content) where T : new()
{
using (MemoryStream stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(content)))
{
XmlSerializer xmlFormat = new XmlSerializer(typeof(T));
return (T)xmlFormat.Deserialize(stream);
}
}
public static T FileToObject<T>(string fileName) where T : new()
{
string CurrentXMLPath = ConfigurationManager.AppSettings["CurrentXMLPath"];
fileName = Path.Combine(CurrentXMLPath, @"Student.xml");
using (Stream fStream = new FileStream(fileName, FileMode.Open, FileAccess.ReadWrite))
{
XmlSerializer xmlFormat = new XmlSerializer(typeof(T));
return (T)xmlFormat.Deserialize(fStream);
}
}
}
- 自己实现序列化
public static class xHelper
{
public static string ParseToXml<T>(this T model, string fatherNodeName)
{
var xmldoc = new XmlDocument();
var modelNode = xmldoc.CreateElement(fatherNodeName);
xmldoc.AppendChild(modelNode);
if (model != null)
{
foreach (PropertyInfo property in model.GetType().GetProperties())
{
var attribute = xmldoc.CreateElement(property.Name);
if (property.GetValue(model, null) != null)
attribute.InnerText = property.GetValue(model, null).ToString();
modelNode.AppendChild(attribute);
}
}
return xmldoc.OuterXml;
}
public static T ParseToModel<T>(this string xml, string fatherNodeName = "body") where T : class ,new()
{
if (string.IsNullOrEmpty(xml))
return default(T);
var xmldoc = new XmlDocument();
xmldoc.LoadXml(xml);
T model = new T();
var attributes = xmldoc.SelectSingleNode(fatherNodeName).ChildNodes;
foreach (XmlNode node in attributes)
{
foreach (var property in model.GetType().GetProperties().Where(property => node.Name == property.Name))
{
if (!string.IsNullOrEmpty(node.InnerText))
{
property.SetValue(model,
property.PropertyType == typeof(Guid)
? new Guid(node.InnerText)
: Convert.ChangeType(node.InnerText, property.PropertyType));
}
else
{
property.SetValue(model, null);
}
}
}
return model;
}
public static List<T> XmlToObjList<T>(this string xml, string headtag)
where T : new()
{
var list = new List<T>();
XmlDocument doc = new XmlDocument();
PropertyInfo[] propinfos = null;
doc.LoadXml(xml);
XmlNodeList nodelist = doc.SelectNodes(headtag);
foreach (XmlNode node in nodelist)
{
T entity = new T();
if (propinfos == null)
{
Type objtype = entity.GetType();
propinfos = objtype.GetProperties();
}
foreach (PropertyInfo propinfo in propinfos)
{
string name = propinfo.Name.Substring(0, 1) + propinfo.Name.Substring(1, propinfo.Name.Length - 1);
XmlNode cnode = node.SelectSingleNode(name);
string v = cnode.InnerText;
if (v != null)
propinfo.SetValue(entity, Convert.ChangeType(v, propinfo.PropertyType), null);
}
list.Add(entity);
}
return list;
}
}
JsonHelper
public class JsonHelper
{
#region Json
public static string ObjectToString<T>(T obj)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
return jss.Serialize(obj);
}
public static T StringToObject<T>(string content)
{
JavaScriptSerializer jss = new JavaScriptSerializer();
return jss.Deserialize<T>(content);
}
public static string ToJson<T>(T obj)
{
return JsonConvert.SerializeObject(obj);
}
public static T ToObject<T>(string content)
{
return JsonConvert.DeserializeObject<T>(content);
}
#endregion Json
}
LinqToXML
public class LinqToXml
{
private static void CreateXmlFile(string xmlPath)
{
try
{
XDocument myXDoc = new XDocument(
new XElement("Users",
new XElement("User", new XAttribute("ID", "111111"),
new XElement("name", "EricSun"),
new XElement("password", "123456"),
new XElement("description", "Hello I'm from Dalian")),
new XElement("User", new XAttribute("ID", "222222"),
new XElement("name", "Ray"),
new XElement("password", "654321"),
new XElement("description", "Hello I'm from Jilin"))));
myXDoc.Save(xmlPath);
string aa = myXDoc.ToString();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
private static void GetXmlNodeInformation(string xmlPath)
{
try
{
XElement rootNode = XElement.Load(xmlPath);
IEnumerable<XElement> targetNodes = from target in rootNode.Descendants("name")
select target;
foreach (XElement node in targetNodes)
{
Console.WriteLine("name = {0}", node.Value);
}
IEnumerable<XElement> myTargetNodes = from myTarget in rootNode.Descendants("User")
where myTarget.Attribute("ID").Value.Equals("111111")
&& myTarget.HasElements
select myTarget;
foreach (XElement node in myTargetNodes)
{
Console.WriteLine("name = {0}", node.Element("name").Value);
Console.WriteLine("password = {0}", node.Element("password").Value);
Console.WriteLine("description = {0}", node.Element("description").Value);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
public static void ModifyXmlNodeInformation(string xmlPath)
{
try
{
XElement rootNode = XElement.Load(xmlPath);
IEnumerable<XElement> targetNodes = from target in rootNode.Descendants("User")
where target.Attribute("ID").Value == "222222"
|| target.Attribute("ID").Value.Equals("777777")
select target;
foreach (XElement node in targetNodes)
{
node.Element("description").SetValue("Hello, I'm from USA.");
}
rootNode.Save(xmlPath);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
private static void AddXmlNodeInformation(string xmlPath)
{
try
{
XElement rootNode = XElement.Load(xmlPath);
XElement newNode = new XElement("User", new XAttribute("ID", "999999"),
new XElement("name", "Rose"),
new XElement("password", "456123"),
new XElement("description", "Hello, I'm from UK."));
rootNode.Add(newNode);
rootNode.Save(xmlPath);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
private static void DeleteXmlNodeInformation(string xmlPath)
{
try
{
XElement rootNode = XElement.Load(xmlPath);
IEnumerable<XElement> targetNodes = from target in rootNode.Descendants("User")
where target.Attribute("ID").Value.Equals("999999")
select target;
targetNodes.Remove();
rootNode.Save(xmlPath);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
}
}
ImageHelper
public class ImageHelper
{
private static string ImagePath = ConfigurationManager.AppSettings["ImagePath"]
private static string VerifyPath = ConfigurationManager.AppSettings["ImagePath"]
//绘图的原理很简单:Bitmap就像一张画布,Graphics如同画图的手,把Pen或Brush等绘图对象画在Bitmap这张画布上
/// <summary>
/// 画验证码
/// </summary>
public static void Drawing()
{
Bitmap bitmapobj = new Bitmap(100, 100)
//在Bitmap上创建一个新的Graphics对象
Graphics g = Graphics.FromImage(bitmapobj)
//创建绘画对象,如Pen,Brush等
Pen redPen = new Pen(Color.Red, 8)
g.Clear(Color.White)
//绘制图形
g.DrawLine(redPen, 50, 20, 500, 20)
g.DrawEllipse(Pens.Black, new Rectangle(0, 0, 200, 100))
g.DrawArc(Pens.Black, new Rectangle(0, 0, 100, 100), 60, 180)
g.DrawLine(Pens.Black, 10, 10, 100, 100)
g.DrawRectangle(Pens.Black, new Rectangle(0, 0, 100, 200))
g.DrawString("我爱北京天安门", new Font("微软雅黑", 12), new SolidBrush(Color.Red), new PointF(10, 10))
//g.DrawImage(
if (!Directory.Exists(ImagePath))
Directory.CreateDirectory(ImagePath)
bitmapobj.Save(ImagePath + "pic1.jpg", ImageFormat.Jpeg)
//释放所有对象
bitmapobj.Dispose()
g.Dispose()
}
public static void VerificationCode()
{
Bitmap bitmapobj = new Bitmap(300, 300)
//在Bitmap上创建一个新的Graphics对象
Graphics g = Graphics.FromImage(bitmapobj)
g.DrawRectangle(Pens.Black, new Rectangle(0, 0, 150, 50))
g.FillRectangle(Brushes.White, new Rectangle(1, 1, 149, 49))
g.DrawArc(Pens.Blue, new Rectangle(10, 10, 140, 10), 150, 90)
string[] arrStr = new string[] { "我", "们", "孝", "行", "白", "到", "国", "中", "来", "真" }
Random r = new Random()
int i
for (int j = 0
{
i = r.Next(10)
g.DrawString(arrStr[i], new Font("微软雅黑", 15), Brushes.Red, new PointF(j * 30, 10))
}
bitmapobj.Save(Path.Combine(VerifyPath, "Verif.jpg"), ImageFormat.Jpeg)
bitmapobj.Dispose()
g.Dispose()
}
/// <summary>
/// 按比例缩放,图片不会变形,会优先满足原图和最大长宽比例最高的一项
/// </summary>
/// <param name="oldPath"></param>
/// <param name="newPath"></param>
/// <param name="maxWidth"></param>
/// <param name="maxHeight"></param>
public static void CompressPercent(string oldPath, string newPath, int maxWidth, int maxHeight)
{
Image _sourceImg = Image.FromFile(oldPath)
double _newW = (double)maxWidth
double _newH = (double)maxHeight
double percentWidth = (double)_sourceImg.Width > maxWidth ? (double)maxWidth : (double)_sourceImg.Width
if ((double)_sourceImg.Height * (double)percentWidth / (double)_sourceImg.Width > (double)maxHeight)
{
_newH = (double)maxHeight
_newW = (double)maxHeight / (double)_sourceImg.Height * (double)_sourceImg.Width
}
else
{
_newW = percentWidth
_newH = (percentWidth / (double)_sourceImg.Width) * (double)_sourceImg.Height
}
System.Drawing.Image bitmap = new System.Drawing.Bitmap((int)_newW, (int)_newH)
Graphics g = Graphics.FromImage(bitmap)
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
g.Clear(Color.Transparent)
g.DrawImage(_sourceImg, new Rectangle(0, 0, (int)_newW, (int)_newH), new Rectangle(0, 0, _sourceImg.Width, _sourceImg.Height), GraphicsUnit.Pixel)
_sourceImg.Dispose()
g.Dispose()
bitmap.Save(newPath, System.Drawing.Imaging.ImageFormat.Jpeg)
bitmap.Dispose()
}
/// <summary>
/// 按照指定大小对图片进行缩放,可能会图片变形
/// </summary>
/// <param name="oldPath"></param>
/// <param name="newPath"></param>
/// <param name="newWidth"></param>
/// <param name="newHeight"></param>
public static void ImageChangeBySize(string oldPath, string newPath, int newWidth, int newHeight)
{
Image sourceImg = Image.FromFile(oldPath)
System.Drawing.Image bitmap = new System.Drawing.Bitmap(newWidth, newHeight)
Graphics g = Graphics.FromImage(bitmap)
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality
g.Clear(Color.Transparent)
g.DrawImage(sourceImg, new Rectangle(0, 0, newWidth, newHeight), new Rectangle(0, 0, sourceImg.Width, sourceImg.Height), GraphicsUnit.Pixel)
sourceImg.Dispose()
g.Dispose()
bitmap.Save(newPath, System.Drawing.Imaging.ImageFormat.Jpeg)
bitmap.Dispose()
}
//添加水印
//二维码 QRCode
}