重庆万州中学排名:ASP.NET中类能写文件管理吗?类里面查询的目录为什么不是当前页面而是VS软件所在页面的。

来源:百度文库 编辑:杭州交通信息网 时间:2024/04/28 13:27:13
在.NET里面我写了一个类用来管理文件,生成静态页面的,因为在类里面用到了System.IO.File里面以及System.Web.HttpContext.Current里面的东西,为什么找到的目录不是当前网页的,而是VS软件所在页的目录呢??有没有办法实现用类写成的文件管理呢
下面是我的代码
namespace File
{
public class FileManagement
{
public static bool WriteFile(string FilePath, string FileName, string Text)
{
System.IO.
string FileInfo;
if (CheckDirectory(FilePath) && CheckFile(FilePath, FileName))
{
FileInfo = System.Web.HttpContext.Current.Server.MapPath(FilePath) + "\\" + FileName;
StreamWriter Writer = new StreamWriter(FileInfo, false, System.Text.Encoding.GetEncoding("GB2312"));
try
{
Writer.WriteLine(Text);
Writer.Flush();
Writer.Close();
return true;
}
catch (Exception ex)
{
System.Web.HttpContext.Current.Response.Write("<script>静态页面创建失败!</script>");
return false;
}
}
else return false;
}
public static bool CheckDirectory(string DirtoryPath)
{
string Path = System.Web.HttpContext.Current.Server.MapPath(DirtoryPath);
if (System.IO.Directory.Exists(Path))
{
return true;
}
else
{
System.IO.Directory.CreateDirectory(Path);
return true;
}
}
public static bool CheckFile(string FilePath, string FileName)
{
string FileInfo = FilePath + "\\" + FileName;
CheckDirectory(FilePath);
if (System.IO.File.Exists(FileInfo))
{
return true;
}
else
{
System.IO.File.Create(FileInfo);
return true;
}
}
public static bool DeleteFile(string FilePath, string FileName)
{
string Path;
if (CheckDirectory(FilePath) && CheckFile(FilePath,FileName))
{
Path = System.Web.HttpContext.Current.Server.MapPath(FilePath);
try
{
System.IO.File.Delete(Path);
return true;
}
catch
{
System.Web.HttpContext.Current.Response.Write("<script>文件删除失败!</script>");
return false;
}
}
else return false;
}
}
}