C# Directory FileStream StreamWriter
2015-10-14 18:13
459 查看
using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.IO; using System.Linq; using System.Web; using System.Web.Mvc; namespace WebApplication1.Controllers { public class HomeController : Controller { public ActionResult Index() { string message = string.Empty; string folderPath1 = System.AppDomain.CurrentDomain.BaseDirectory + "\\log"; string folderPath2 = System.IO.Directory.GetCurrentDirectory() + "\\log"; string folderPath3 = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName + "\\log"; string folderPath4 = System.Environment.CurrentDirectory + "\\log"; string folderPath5 = Server.MapPath("\\log"); string fileName = "\\" + DateTime.Now.ToShortDateString() + ".log"; string filePath = folderPath1 + fileName; try { ///检查“文件夹”是否存在 if (!System.IO.Directory.Exists(folderPath1)) { Directory.CreateDirectory(folderPath1); //Directory.Delete(""); //Directory.Move("原地址", "新地址"); } ///检查“文件”是否存在 if (!System.IO.File.Exists(filePath)) { FileStream fs = new FileStream(filePath, FileMode.Create); StreamWriter sw = new StreamWriter(fs); sw.WriteLine(DateTime.Now.ToString() + " " + message + "\t"); sw.Close(); fs.Close(); } else { #region 方式一 ///FileMode.Append 若存在文件,则打开该文件并查找到文件尾,或者创建一个新文件 ///FileMode.Create 指定操作系统应创建新文件 ///FileMode.CreateNew 指定操作系统应创建新文件 ///FileMode.Open 指定操作系统应打开现有文件 ///FileMode.OpenOrCreate (如果文件存在);否则,应创建新文件 ///FileMode.Truncate FileStream fs = new FileStream(filePath, FileMode.Append); //FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write); StreamWriter sw1 = new StreamWriter(fs); sw1.WriteLine(DateTime.Now.ToString() + " " + message); sw1.Close(); fs.Close(); #endregion #region 方式二 ///设置成true会连续写内容 ///设置成false会覆盖原有内容 StreamWriter sw = new StreamWriter(filePath, true); sw.WriteLine(DateTime.Now.ToString() + " " + message); sw.Close(); #endregion } } catch (Exception) { } return View(); } public ActionResult Stock() { //byte[] bts; //#region //#endregion 接收流文件 //Image img = new Bitmap("https://***.***.com/I2AdminNew/GetBackgroundImageHandler.ashx"); //MemoryStream ms = new MemoryStream(); //img.Save(ms, ImageFormat.Bmp); //bts = ms.ToArray(); //ms.Close(); //#region 流文件转换为图片 //MemoryStream ms1 = new MemoryStream(bts); ////Bitmap img1 = new Bitmap(ms1); //Image img1 = Image.FromStream(ms1); //ms1.Close(); //#endregion return View(); } } }
相关文章推荐
- c#中文字大全,转换为字符串格式
- C#的WINFOR录窗体中生成验证码
- C#排序算法小结
- C#中子类调用父类的实现方法
- C#中Equals方法的常见误解
- C#: 一个方法执行超时 timeout 检查的实现
- c#语音识别与合成
- C# 给枚举定义DescriptionAttribute,把枚举转换为键值对
- C#编程自学之流程控制语句
- C# 实现文件下载的几种方式
- BC#59 The Highest Mark
- C# 文件压缩与解压(ZIP格式)
- C# 反射 通过类名创建类实例
- windbg查找c#高内存原因
- C# 生成和读取Xml文档
- C#基础,值类型与引用类型,父类与子类
- (转)用C#一步步写串口通信
- (转)用C#一步步写串口通信
- C#随机数生成
- 检查c#代码内存泄露工具-CLR Profiler