公用帮助文件处理类

来源:互联网 发布:如何看懂单片机原理图 编辑:程序博客网 时间:2024/05/01 00:05
 
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 my.IO
{
    
/// <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;
        }

    }

}

原创粉丝点击