3个回答
2013-06-10
展开全部
用这段代码试试:using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.IO;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// MyFile主要针对文件做操作
/// </summary>
public class MyFile{
/// <summary>
/// 检查目录是否存在
/// </summary>
/// <param name="FolderName">目录地址</param>
/// <returns></returns>
public static bool DirectoryExists(string FolderName)
{
return Directory.Exists(HttpContext.Current.Server.MapPath(FolderName));
}
/// <summary>
/// 按日期生成文件名
/// </summary>
/// <param name="fname">所要生成文件所在地址</param>
/// <returns></returns>
public static string CFileName(string fname)
{
Random x = new Random();
string _x = x.Next(1, 999).ToString();
string _FileName = DateTime.Now.ToString("yyMMddhhmmss") + _x + fname.Substring(fname.LastIndexOf("."));
return _FileName;
}
/// <summary>
/// 读取模版并生成html文件
/// </summary>
/// <param name="HtmlPath">所要写文件的地址</param>
/// <param name="TemplateFile">模板地址</param>
/// <param name="sItem">所要替换的参数</param>
/// <param name="sValue">替换内容</param>
/// <param name="oldFile">如不覆盖,则为空</param>
/// <returns>文件路径</returns>
public static string WriteHtml(string HtmlPath, string TemplateFile, string sItem, string sValue,string oldFile)
{
try
{
//目录不存在就创建文件夹
if (!DirectoryExists(HtmlPath)){
Directory.CreateDirectory(HttpContext.Current.Server.MapPath(HtmlPath));
}
//读取模版内容
string tep;
StreamReader rtp = new StreamReader(HttpContext.Current.Server.MapPath(TemplateFile), System.Text.Encoding.Default);
tep = rtp.ReadToEnd();
//将项和值分解数组
string[] sItems = sItem.Split('|');
string[] sValues = sValue.Split('|');
for (int i = 0;i < sItems.Length;i++){
tep = tep.Replace(sItems[i], sValues[i]);
}
//写出文件
string Npath = "";
StreamWriter wtp;
if (oldFile == ""){
string NewFileName = CFileName(".html");
wtp = new StreamWriter(HttpContext.Current.Server.MapPath(HtmlPath) + @"\" + NewFileName, false, System.Text.Encoding.Default);
Npath = HtmlPath + NewFileName;
}
else
{
wtp = new StreamWriter(HttpContext.Current.Server.MapPath(oldFile), false, System.Text.Encoding.Default);
Npath = oldFile;
}
//写出文件
wtp.Write(tep);
rtp.Close();
wtp.Close();
return Npath;
}
catch(Exception e)
{
return e.Message.ToString();
}
}
/// <summary>
/// 删除文件
/// </summary>
/// <param name="FilePath">文件所在的相对位置</param>
public static void DelFile(string FilePath){
try{
File.Delete(HttpContext.Current.Server.MapPath(FilePath));
}
catch{
//Cstr.JsMsg("一个或多个文件未被删除!");
}
}
/// <summary>
/// 检查目录是否存在,不存在就随机生成并返回
/// 生成目录为日期
/// </summary>
/// <param name="Fpath">所检测目录</param>
/// <returns></returns>
public static string GetFolder(string Fpath){
if(!DirectoryExists(Fpath+DateTime.Now.ToString("yyMMdd"))){
Directory.CreateDirectory(HttpContext.Current.Server.MapPath(Fpath)+DateTime.Now.ToString("yyMMdd"));
}
return Fpath+DateTime.Now.ToString("yyMMdd");
}
/// <summary>
/// 检查原始目录是否存在,不存在就生成并返回
/// 生成目录为指定的
/// </summary>
/// <param name="Fpath">指定路径</param>
/// <param name="OldPath">指定目录名</param>
/// <returns></returns>
public static string GetFolder(string Fpath,string OldPath)
{
if (!DirectoryExists(Fpath + OldPath))
{
Directory.CreateDirectory(HttpContext.Current.Server.MapPath(Fpath) + OldPath);
}
return Fpath + OldPath;
}
}
using System.Data;
using System.Configuration;
using System.Web;
using System.IO;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// MyFile主要针对文件做操作
/// </summary>
public class MyFile{
/// <summary>
/// 检查目录是否存在
/// </summary>
/// <param name="FolderName">目录地址</param>
/// <returns></returns>
public static bool DirectoryExists(string FolderName)
{
return Directory.Exists(HttpContext.Current.Server.MapPath(FolderName));
}
/// <summary>
/// 按日期生成文件名
/// </summary>
/// <param name="fname">所要生成文件所在地址</param>
/// <returns></returns>
public static string CFileName(string fname)
{
Random x = new Random();
string _x = x.Next(1, 999).ToString();
string _FileName = DateTime.Now.ToString("yyMMddhhmmss") + _x + fname.Substring(fname.LastIndexOf("."));
return _FileName;
}
/// <summary>
/// 读取模版并生成html文件
/// </summary>
/// <param name="HtmlPath">所要写文件的地址</param>
/// <param name="TemplateFile">模板地址</param>
/// <param name="sItem">所要替换的参数</param>
/// <param name="sValue">替换内容</param>
/// <param name="oldFile">如不覆盖,则为空</param>
/// <returns>文件路径</returns>
public static string WriteHtml(string HtmlPath, string TemplateFile, string sItem, string sValue,string oldFile)
{
try
{
//目录不存在就创建文件夹
if (!DirectoryExists(HtmlPath)){
Directory.CreateDirectory(HttpContext.Current.Server.MapPath(HtmlPath));
}
//读取模版内容
string tep;
StreamReader rtp = new StreamReader(HttpContext.Current.Server.MapPath(TemplateFile), System.Text.Encoding.Default);
tep = rtp.ReadToEnd();
//将项和值分解数组
string[] sItems = sItem.Split('|');
string[] sValues = sValue.Split('|');
for (int i = 0;i < sItems.Length;i++){
tep = tep.Replace(sItems[i], sValues[i]);
}
//写出文件
string Npath = "";
StreamWriter wtp;
if (oldFile == ""){
string NewFileName = CFileName(".html");
wtp = new StreamWriter(HttpContext.Current.Server.MapPath(HtmlPath) + @"\" + NewFileName, false, System.Text.Encoding.Default);
Npath = HtmlPath + NewFileName;
}
else
{
wtp = new StreamWriter(HttpContext.Current.Server.MapPath(oldFile), false, System.Text.Encoding.Default);
Npath = oldFile;
}
//写出文件
wtp.Write(tep);
rtp.Close();
wtp.Close();
return Npath;
}
catch(Exception e)
{
return e.Message.ToString();
}
}
/// <summary>
/// 删除文件
/// </summary>
/// <param name="FilePath">文件所在的相对位置</param>
public static void DelFile(string FilePath){
try{
File.Delete(HttpContext.Current.Server.MapPath(FilePath));
}
catch{
//Cstr.JsMsg("一个或多个文件未被删除!");
}
}
/// <summary>
/// 检查目录是否存在,不存在就随机生成并返回
/// 生成目录为日期
/// </summary>
/// <param name="Fpath">所检测目录</param>
/// <returns></returns>
public static string GetFolder(string Fpath){
if(!DirectoryExists(Fpath+DateTime.Now.ToString("yyMMdd"))){
Directory.CreateDirectory(HttpContext.Current.Server.MapPath(Fpath)+DateTime.Now.ToString("yyMMdd"));
}
return Fpath+DateTime.Now.ToString("yyMMdd");
}
/// <summary>
/// 检查原始目录是否存在,不存在就生成并返回
/// 生成目录为指定的
/// </summary>
/// <param name="Fpath">指定路径</param>
/// <param name="OldPath">指定目录名</param>
/// <returns></returns>
public static string GetFolder(string Fpath,string OldPath)
{
if (!DirectoryExists(Fpath + OldPath))
{
Directory.CreateDirectory(HttpContext.Current.Server.MapPath(Fpath) + OldPath);
}
return Fpath + OldPath;
}
}
Storm代理
2023-07-25 广告
2023-07-25 广告
StormProxies是全球大数据IP资源服务商,其住宅代理网络由真实的家庭住宅IP组成,可为企业或个人提供满足各种场景的代理产品。点击免费测试(注册即送1G流量)StormProxies有哪些优势?1、IP+端口提取形式,不限带宽,IP...
点击进入详情页
本回答由Storm代理提供
2013-06-10
展开全部
哇哦,那么长的答案哦,好好学习一下,不知道有没有用呢?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-06-10
展开全部
原理就是把页面读取出来.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询