zoukankan      html  css  js  c++  java
  • .net工具类——文件操作

            #region 文件操作
    
            /// <summary>
            /// 删除单个文件
            /// </summary>
            /// <param name="_filepath">文件相对路径</param>
            public static bool DeleteFile(string _filepath)
            {
                if (string.IsNullOrEmpty(_filepath))
                {
                    return false;
                }
                string fullpath = GetMapPath(_filepath);
                if (File.Exists(fullpath))
                {
                    File.Delete(fullpath);
                    return true;
                }
                return false;
            }
    
            /// <summary>
            /// 删除上传的文件(及缩略图)
            /// </summary>
            /// <param name="_filepath"></param>
            public static void DeleteUpFile(string _filepath)
            {
                if (string.IsNullOrEmpty(_filepath))
                {
                    return;
                }
                string fullpath = GetMapPath(_filepath); //原图
                if (File.Exists(fullpath))
                {
                    File.Delete(fullpath);
                }
                if (_filepath.LastIndexOf("/") >= 0)
                {
                    string thumbnailpath = _filepath.Substring(0, _filepath.LastIndexOf("/")) + "mall_" + _filepath.Substring(_filepath.LastIndexOf("/") + 1);
                    string fullTPATH = GetMapPath(thumbnailpath); //宿略图
                    if (File.Exists(fullTPATH))
                    {
                        File.Delete(fullTPATH);
                    }
                }
            }
    
            /// <summary>
            /// 删除指定文件夹
            /// </summary>
            /// <param name="_dirpath">文件相对路径</param>
            public static bool DeleteDirectory(string _dirpath)
            {
                if (string.IsNullOrEmpty(_dirpath))
                {
                    return false;
                }
                string fullpath = GetMapPath(_dirpath);
                if (Directory.Exists(fullpath))
                {
                    Directory.Delete(fullpath, true);
                    return true;
                }
                return false;
            }
    
            /// <summary>
            /// 删除指定文件夹
            /// </summary>
            /// <param name="_dirpath">物理路径</param>
            public static bool DeleteDirectory_PhysicalPath(string _physicalpath)
            {
                if (string.IsNullOrEmpty(_physicalpath))
                {
                    return false;
                }
                if (Directory.Exists(_physicalpath))
                {
                    Directory.Delete(_physicalpath, true);
                    return true;
                }
                return false;
            }
    
            /// <summary>
            /// 修改指定文件夹名称
            /// </summary>
            /// <param name="old_dirpath">旧相对路径</param>
            /// <param name="new_dirpath">新相对路径</param>
            /// <returns>bool</returns>
            public static bool MoveDirectory(string old_dirpath, string new_dirpath)
            {
                if (string.IsNullOrEmpty(old_dirpath))
                {
                    return false;
                }
                string fulloldpath = GetMapPath(old_dirpath);
                string fullnewpath = GetMapPath(new_dirpath);
                if (Directory.Exists(fulloldpath))
                {
                    Directory.Move(fulloldpath, fullnewpath);
                    return true;
                }
                return false;
            }
    
            /// <summary>
            /// 返回文件大小KB
            /// </summary>
            /// <param name="_filepath">文件相对路径</param>
            /// <returns>int</returns>
            public static int GetFileSize(string _filepath)
            {
                if (string.IsNullOrEmpty(_filepath))
                {
                    return 0;
                }
                string fullpath = GetMapPath(_filepath);
                if (File.Exists(fullpath))
                {
                    FileInfo fileInfo = new FileInfo(fullpath);
                    return ((int)fileInfo.Length) / 1024;
                }
                return 0;
            }
    
            /// <summary>
            /// 返回文件扩展名,不含“.”
            /// </summary>
            /// <param name="_filepath">文件全名称</param>
            /// <returns>string</returns>
            public static string GetFileExt(string _filepath)
            {
                if (string.IsNullOrEmpty(_filepath))
                {
                    return "";
                }
                if (_filepath.LastIndexOf(".") > 0)
                {
                    return _filepath.Substring(_filepath.LastIndexOf(".") + 1); //文件扩展名,不含“.”
                }
                return "";
            }
    
            /// <summary>
            /// 返回文件名,不含路径
            /// </summary>
            /// <param name="_filepath">文件相对路径</param>
            /// <returns>string</returns>
            public static string GetFileName(string _filepath)
            {
                return _filepath.Substring(_filepath.LastIndexOf(@"/") + 1);
            }
    
            /// <summary>
            /// 文件是否存在
            /// </summary>
            /// <param name="_filepath">文件相对路径</param>
            /// <returns>bool</returns>
            public static bool FileExists(string _filepath)
            {
                string fullpath = GetMapPath(_filepath);
                if (File.Exists(fullpath))
                {
                    return true;
                }
                return false;
            }
            public static string GetFile(string filepath)
            {
                string json = string.Empty;
                using (FileStream fs = new FileStream(filepath, FileMode.Open, System.IO.FileAccess.Read, FileShare.ReadWrite))
                {
                    using (StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("gb2312")))
                    {
                        json = sr.ReadToEnd().ToString();
                    }
                }
                return json;
            }
    
            ///// <summary>
            ///// 获得远程字符串
            ///// </summary>
            //public static string GetDomainStr(string key, string uriPath)
            //{
            //    string result = CacheHelper.GetCache(key) as string;
            //    if (result == null)
            //    {
            //        System.Net.WebClient client = new System.Net.WebClient();
            //        try
            //        {
            //            client.Encoding = System.Text.Encoding.UTF8;
            //            result = client.DownloadString(uriPath);
            //        }
            //        catch
            //        {
            //            result = "暂时无法连接!";
            //        }
            //        CacheHelper.SetCache(key, result, 60);
            //    }
    
            //    return result;
            //}
    
            #endregion
  • 相关阅读:
    微软发布最新Windows Azure for Windows Phone 7工具包
    Programming_Windows_Phone_7 读书笔记改版说明
    Windows Phone 7 编程实践 基础篇—多点触控示例(Silverlight)
    Windows Phone 7编程实践—推送通知_剖析推送通知实现架构
    Windows Phone 7 页面导航以及异常处理
    Programming_Windows_Phone_7 读书笔记—基础篇(触控)
    PROGRAMMING_WINDOWS_PHONE_7 读书笔记—基础篇(横向和纵向视图)
    Windows Phone 7 编程实践–XNA变身记
    [转载Terrence Dorsey]Windows Phone 7开发工具和资源
    Oracle8i 函数索引实验
  • 原文地址:https://www.cnblogs.com/amusement1992/p/13496253.html
Copyright © 2011-2022 走看看