公用帮助文件处理类

 
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;
        }

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值