Exemplo n.º 1
0
        /// <summary>
        /// 获取目录中所有文件路径
        /// </summary>
        /// <param name="dir">目录</param>
        /// <param name="patt">匹配正则(如:*.(txt|log))</param>
        /// <returns></returns>
        public static List <string> GetFiles(string dir, string patt)
        {
            string pattern = "(?i)^" + patt.Replace(".", @"\.") + "$";

            Regex reg = new Regex(pattern);

            List <string> list = new List <string>();

            try
            {
                string[] files = Directory.GetFiles(DNTUtils.GetMapPath(dir));

                foreach (string file in files)
                {
                    if (reg.IsMatch(file))
                    {
                        list.Add(file);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return(list);
        }
Exemplo n.º 2
0
 /// <summary>
 /// 获取指定目录下所有文件
 /// </summary>
 /// <param name="dir">目录</param>
 /// <returns></returns>
 public static string[] GetFiles(string dir)
 {
     try
     {
         return(Directory.GetFiles(DNTUtils.GetMapPath(dir)));
     }
     catch (DirectoryNotFoundException ex)
     {
         throw ex;
     }
 }
Exemplo n.º 3
0
 /// <summary>
 /// 返回文件行的数组
 /// </summary>
 /// <param name="path">文件路径</param>
 /// <param name="encode">编码方式</param>
 /// <returns></returns>
 public static string[] ReadLines(string path, Encoding encode)
 {
     try
     {
         return(File.ReadAllLines(DNTUtils.GetMapPath(path), encode));
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Exemplo n.º 4
0
 /// <summary>
 /// 根据文件路径,或文件夹路径,创建文件夹
 /// </summary>
 /// <param name="dir"></param>
 public static void IfNotExistsCreateDir(string dir)
 {
     dir = DNTUtils.GetMapPath(dir);
     if (dir.Contains("."))
     {
         dir = dir.Substring(0, dir.LastIndexOf("\\"));
     }
     if (!Directory.Exists(dir))
     {
         Directory.CreateDirectory(dir);
     }
 }
Exemplo n.º 5
0
 /// <summary>
 /// 删除指定文件
 /// </summary>
 /// <param name="path">路径</param>
 /// <returns></returns>
 public static bool Delete(string path)
 {
     try
     {
         path = DNTUtils.GetMapPath(path);
         if (File.Exists(path))
         {
             File.Delete(path);
         }
         return(true);
     }
     catch (Exception ex)
     {
         throw ex;
     }
 }
Exemplo n.º 6
0
        /// <summary>
        /// 删除图片
        /// </summary>
        /// <param name="path"></param>
        public static void DeleteImg(string path)
        {
            if (string.IsNullOrEmpty(path))
            {
                return;
            }
            string   serverpath = DNTUtils.GetMapPath(path);
            FileInfo file       = new FileInfo(serverpath);

            if (File.Exists(serverpath))
            {
                File.Delete(serverpath);
            }
            if (File.Exists(serverpath.Replace("_s", "")))
            {
                File.Delete(serverpath.Replace("_s", ""));
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// 获得当前页面客户端的IP
        /// </summary>
        /// <returns>当前页面客户端的IP</returns>
        public static string GetIP()
        {
            string result = String.Empty;

            result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (string.IsNullOrEmpty(result))
            {
                result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
            }
            if (string.IsNullOrEmpty(result))
            {
                result = HttpContext.Current.Request.UserHostAddress;
            }
            if (string.IsNullOrEmpty(result) || !DNTUtils.IsIP(result))
            {
                return("127.0.0.1");
            }
            return(result);
        }
Exemplo n.º 8
0
        /// <summary>
        /// 读取文件
        /// </summary>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public Stream FileToStream(string fileName)
        {
            // 打开文件
            FileStream fileStream = new FileStream(DNTUtils.GetMapPath(fileName), FileMode.Open, FileAccess.Read, FileShare.Read);

            // 读取文件的 byte[]
            byte[] bytes = new byte[fileStream.Length];
            try
            {
                fileStream.Read(bytes, 0, bytes.Length);
            }
            finally
            {
                fileStream.Close();
            }
            // 把 byte[] 转换成 Stream
            Stream stream = new MemoryStream(bytes);

            return(stream);
        }
Exemplo n.º 9
0
        /// <summary>
        /// 保存文件
        /// </summary>
        /// <param name="stream"></param>
        /// <param name="fileName"></param>
        public static void StreamToFile(Stream stream, string fileName)
        {
            // 把 Stream 转换成 byte[]
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            // 设置当前流的位置为流的开始
            stream.Seek(0, SeekOrigin.Begin);
            // 把 byte[] 写入文件
            FileStream   fs = new FileStream(DNTUtils.GetMapPath(fileName), FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);

            try
            {
                bw.Write(bytes);
            }
            finally
            {
                bw.Close();
                fs.Close();
            }
        }
Exemplo n.º 10
0
 /// <summary>
 /// getBinaryFile:返回所给文件路径的字节数组。
 /// </summary>
 /// <param name="filename"></param>
 /// <returns></returns>
 public static byte[] getBinaryFile(string filename)
 {
     filename = DNTUtils.GetMapPath(filename);
     if (File.Exists(filename))
     {
         try
         {
             ///打开现有文件以进行读取。
             FileStream s = File.OpenRead(filename);
             return(ConvertStreamToByteBuffer(s));
         }
         catch
         {
             return(new byte[0]);
         }
     }
     else
     {
         return(new byte[0]);
     }
 }
Exemplo n.º 11
0
        /// <summary>
        /// 向文件追加内容(指定编码)
        /// </summary>
        /// <param name="path">路径</param>
        /// <param name="content">内容</param>
        /// <param name="encode">编码</param>
        /// <returns></returns>
        public static bool Append(string path, string content, Encoding encode)
        {
            FileStream fs = null;

            for (int i = 0; i < 5; i++)
            {
                try
                {
                    fs = new FileStream(DNTUtils.GetMapPath(path), FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
                    break;
                }
                catch
                {
                    System.Threading.Thread.Sleep(50);
                }
            }
            if (fs == null)
            {
                return(false);
            }
            StreamWriter sw = null;

            try
            {
                sw = new StreamWriter(fs, encode);
                sw.Write(content);
                return(true);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                sw.Close();
                fs.Close();
            }
        }
Exemplo n.º 12
0
        /// <summary>
        /// 读取文件(指定编码)
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="encode">编码方式</param>
        /// <returns></returns>
        public static string Read(string path, Encoding encode)
        {
            FileStream fs = null;

            for (int i = 0; i < 5; i++)
            {
                try
                {
                    fs = new FileStream(DNTUtils.GetMapPath(path), FileMode.Open, FileAccess.Read, FileShare.Read);
                    break;
                }
                catch
                {
                    System.Threading.Thread.Sleep(50);
                }
            }
            if (fs == null)
            {
                return("");
            }
            StreamReader sr = null;

            try
            {
                sr = new StreamReader(fs, encode);
                return(sr.ReadToEnd());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                sr.Close();
                fs.Close();
            }
        }
Exemplo n.º 13
0
 public void getpath()
 {
     xmlpath = DNTUtils.GetMapPath(ConfigUtil.GetConfig("ErrorLogPath") + "\\" + DateTime.Now.ToString("yyyyMMdd") + ".xml");
 }