C# Directory FileStream StreamWriter


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# Directory FileStream StreamWriter)