按照时间创建多层文件夹,存储图片

 
class StoreImage
    {
        static string ImagePath = ConfigurationManager.AppSettings["ImagesPath"].ToString();
        static string Path = ImagePath + "\\BarcodeImages";
        static string YearPath = Path + "\\" + DateTime.Now.Year.ToString() + "年";
        static string MonthPath = YearPath + "\\" + DateTime.Now.Month.ToString()+"月";
        static string DayPath = MonthPath + "\\" + DateTime.Now.Day.ToString()+"日";
        int SerialNo = 0;
        /// <summary>
        /// 创建年月日文件夹
        /// </summary>
        public void CreateFile()
        {
            try
            {
                if (!Directory.Exists(Path))
                {
                    Directory.CreateDirectory(Path);
                }
                
                if (!Directory.Exists(YearPath))
                {
                    Directory.CreateDirectory(YearPath);
                }
                if (!Directory.Exists(MonthPath))
                {
                    Directory.CreateDirectory(MonthPath);
                }
                if (!Directory.Exists(DayPath))
                {
                    Directory.CreateDirectory(DayPath);
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }
        /// <summary>
        /// 将条码作为bitmap的名称保存在当天的文件夹中
        /// </summary>
        /// <param name="barcode"></param>
        /// <param name="bitmap"></param>
        public void SaveImage(string barcode,Bitmap bitmap)
        {
            try
            {
                if (Directory.Exists(DayPath))
                {
                    string Name = DayPath + "\\"+ barcode + ".png";
                    if (File.Exists(Name))
                    {
                        int SerialNo = 1;
                        string NameNew = DayPath + "\\" + barcode +"("+ SerialNo.ToString("000") + ")"+ ".png";
                        while (File.Exists(NameNew))
                        {
                            SerialNo = SerialNo + 1;
                            NameNew = DayPath + "\\" + barcode + "(" + SerialNo.ToString("000") + ")" + ".png";
                        }
                        bitmap.Save(NameNew, System.Drawing.Imaging.ImageFormat.Png);
                    }
                    else
                    {
                        bitmap.Save(Name, System.Drawing.Imaging.ImageFormat.Png);
                    }
                }
                else
                {
                    Directory.CreateDirectory(DayPath);
                    return;
                }      
            }
            catch (Exception ex)
            {
                string s = ex.ToString();
            }

        }


    }

  

猜你喜欢

转载自www.cnblogs.com/-maple-leaf/p/10907461.html