zoukankan      html  css  js  c++  java
  • 实用的文件操作类

    using System;
    using System.Text;
    using System.Xml;
    using System.Data;
    using System.Collections;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Configuration;
    using System.IO;

    /**/////////////////////////////////////////////////////
    ///功能:文件操作类

    ///
    ///
    ////////////////////////////////////////////////////


    namespace EmanUtils
    {
        
    /**//// <summary>
        
    /// 公用帮助文件处理类
        
    /// </summary>

        public class FileHelper
        
    {
            
    /**//// <summary>
            
    /// 构造函数
            
    /// </summary>

            private FileHelper()
            
    {
            }


            上传文件
    上传文件

            
    /**//// <summary>
            
    /// 修改 Web.Config 中 appSettings 节点内节点信息
            
    /// </summary>
            
    /// <param name="nodeName">节点名</param>
            
    /// <param name="nodeKey">节点值</param>
            
    /// <returns>返回操作逻辑值</returns>

            public static bool SetWebConfigValue(string nodeName, string nodeKey)
            
    {
                
    string filename = System.Web.HttpContext.Current.Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath + "/web.config");
                XmlDocument  xmldoc 
    = new XmlDocument();
                xmldoc.Load(filename);
                System.Xml.XmlNode node 
    = xmldoc.SelectSingleNode("configuration//appSettings");
                System.Xml.XmlNodeList nl 
    = node.ChildNodes;
                
    foreach(XmlNode el in nl)
                
    {
                    
    try
                    
    {
                        
    if(el.Attributes["key"].Value == nodeName)
                        
    {
                            el.Attributes[
    "value"].Value = nodeKey;
                        }

                    }
     
                    
    catch
                    
    {
                    }

                }

                
    try
                
    {
                    xmldoc.Save(filename);
                    
    return true;
                }

                
    catch
                
    {
                    
    return false;
                }

            }



            
    /**//// <summary>
            
    /// 从完整路径中提取文件名
            
    /// </summary>

            public static string GetFileName(string path)
            
    {
                
    return Path.GetFileName(path);
            }



            
    /**//// <summary>
            
    /// 新建文件
            
    /// </summary>

            public static void Create(string path)
            
    {
                
    try
                
    {
                    
    // 如果以存在文件就删除
                    if (File.Exists(path))
                    
    {
                        File.Delete(path);
                    }


                    
    //创建文件
                    using (FileStream fs = File.Create(path)) { }
                }

                
    catch (Exception e)
                
    {
                    
    throw e;
                }

            }


            
    /**//// <summary>
            
    /// 重命名文件
            
    /// </summary>

            public static bool ReName(string soucePath, string newPath)
            
    {
                
    try
                
    {
                    File.Move(soucePath, newPath);
                }

                
    catch
                
    {
                    
    return false;
                }


                
    return true;
            }



            
    /**//// <summary>
            
    /// 移动文件
            
    /// </summary>

            public static bool Move(string fromPath, string toPath)
            
    {
                
    return ReName(fromPath, toPath);
            }



            
    /**//// <summary>
            
    /// 删除文件
            
    /// </summary>

            public static bool Delete(string path)
            
    {
                
    try
                
    {
                    File.Delete(path);
                }

                
    catch
                
    {
                    
    return false;
                }


                
    return true;
            }



            
    /**//// <summary>
            
    /// 获取文件扩展名
            
    /// </summary>

            public static string GetFileType(string path)
            
    {
                
    string type = Path.GetExtension(path);
               
    /**//* if (string.IsNullOrEmpty(type))
                {
                    return type;
                }
                
    */

                
    return type.Substring(1);
            }



            
    /**//// <summary>
            
    /// 返回不具有扩展名的指定路径字符串的文件名
            
    /// </summary>

            public static string GetFileNameWithoutExtension(string path)
            
    {
                
    return Path.GetFileNameWithoutExtension(path);
            }

            
            
    /**//// <summary>
            
    /// 判断目标文件夹是否存在
            
    /// </summary>
            
    /// <param name="path">文件夹路径 绝对路径  如: e:\zykey</param>
            
    /// <returns></returns>

            public static bool ifdir(string path)
            
    {
                DirectoryInfo d
    =new DirectoryInfo(path);
                
    return d.Exists;
            }


            
    /**//// <summary>
            
    /// 返回文件是否存在
            
    /// </summary>
            
    /// <param name="filename">文件名</param>
            
    /// <returns>是否存在</returns>

            public static bool FileExists(string filename)
            
    {
                
    return System.IO.File.Exists(filename);
            }




            


            
    /**//// <summary>
            
    /// 以指定的ContentType输出指定文件文件
            
    /// </summary>
            
    /// <param name="filepath">文件路径</param>
            
    /// <param name="filename">输出的文件名</param>
            
    /// <param name="filetype">将文件输出时设置的ContentType</param>

            public static void ResponseFile(string filepath, string  filename, string filetype)
            
    {
                Stream iStream 
    = null;

                
    // 缓冲区为10k
                byte[] buffer = new Byte[10000];

                
    // 文件长度
                int length;

                
    // 需要读的数据长度
                long dataToRead;

                
    try
                
    {
                    
    // 打开文件
                    iStream = new FileStream(filepath, FileMode.Open, FileAccess.Read,FileShare.Read);


                    
    // 需要读的数据长度
                    dataToRead = iStream.Length;

                    HttpContext.Current.Response.ContentType 
    = filetype;
                    HttpContext.Current.Response.AddHeader(
    "Content-Disposition""attachment;filename=" + HttpUtility.UrlEncode(filename.Trim()).Replace("+"" "));

                    
    while (dataToRead > 0)
                    
    {
                        
    // 检查客户端是否还处于连接状态
                        if (HttpContext.Current.Response.IsClientConnected)
                        
    {
                            length 
    = iStream.Read(buffer, 010000);
                            HttpContext.Current.Response.OutputStream.Write(buffer, 
    0, length);
                            HttpContext.Current.Response.Flush();
                            buffer 
    = new Byte[10000];
                            dataToRead 
    = dataToRead - length;
                        }

                        
    else
                        
    {
                            
    // 如果不再连接则跳出死循环
                            dataToRead = -1;
                        }

                    }

                }

                
    catch (Exception ex)
                
    {
                    HttpContext.Current.Response.Write(
    "Error : " + ex.Message);
                }

                
    finally
                
    {
                    
    if (iStream != null)
                    
    {
                        
    // 关闭文件
                        iStream.Close();
                    }

                }

                HttpContext.Current.Response.End();
            }


            
    /**//// <summary>
            
    /// 判断文件名是否为浏览器可以直接显示的图片文件名
            
    /// </summary>
            
    /// <param name="filename">文件名</param>
            
    /// <returns>是否可以直接显示</returns>

            public static bool IsImgFilename(string filename)
            
    {
                filename 
    = filename.Trim();
                
    if (filename.EndsWith("."|| filename.IndexOf("."== -1)
                
    {
                    
    return false;
                }

                
    string extname = filename.Substring(filename.LastIndexOf("."+ 1).ToLower();
                
    return (extname == "jpg" || extname == "jpeg" || extname == "png" || extname == "bmp" || extname == "gif");
            }
                
        }

    }

     
    作者:脑瓜子
             
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
    如果文章对您有用,帮我点一下『好文要顶』,或是点一下『推荐』,这会让我更有动力的为各位读者撰写下一篇文章。
    非常谢谢各位的支持与爱护,在此跟各位说声谢谢!!! ^_^
  • 相关阅读:
    CSS--盒子模型详解
    html元素分类
    HTML语义化(2016/3/16更新)
    如何在线预览github上的html页面?
    【鬼脸原创】谷歌扩展--知乎V2.0
    CSS选择器详解
    HTML基础知识
    python- 日志学习
    python-ddt 数据驱动测试
    python
  • 原文地址:https://www.cnblogs.com/naoguazi/p/1189650.html
Copyright © 2011-2022 走看看