删除文件夹后提示无法从服务器中检索文件夹信息。
删除代码:
public bool DelDirectoryInfo(string path)
{
try
{
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(path);
if (di.Exists)
{
//try
//{
di.Delete();
return true;
//}
//catch (Exception)
//{
// return false;
//}
}
else
{
return false;
}
}
catch (Exception error)
{
return false;
}
}
删除代码:
public bool DelDirectoryInfo(string path)
{
try
{
System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(path);
if (di.Exists)
{
//try
//{
di.Delete();
return true;
//}
//catch (Exception)
//{
// return false;
//}
}
else
{
return false;
}
}
catch (Exception error)
{
return false;
}
}
foreach(string strFile in System.IO.Directory.GetFiles(this.Server.MapPath(@"C:\"),"user*"))
{
System.IO.File.Delete(strFile);
}private void Button1_Click(object sender, System.EventArgs e)
{//新建文件
string path=Server.MapPath("index4.aspx");
//Response.Write(DateTime.Now.ToString("yyyymmdd"));
//获得文件名
int fileLen=path.LastIndexOf(".");
int pathLen=path.Length;
string tempName=path.Substring(fileLen,pathLen-fileLen);
//获取文件路径
int len=path.LastIndexOf(@"\");
string tempPath=path.Substring(0,len)+"\\"+DateTime.Now.ToString("yyyymmddhhssmm")+tempName;
try
{
File.Create(tempPath);
Response.Write("<script>alert('新建文件成功');</script>");
}
catch(Exception ex)
{
Response.Write(ex.ToString());
}
}private void Button2_Click(object sender, System.EventArgs e)
{//复制文件
string path=Server.MapPath("index4.aspx");
//Response.Write(DateTime.Now.ToString("yyyymmdd"));
//获得文件名
int fileLen=path.LastIndexOf(".");
int pathLen=path.Length;
string tempName=path.Substring(fileLen,pathLen-fileLen);
//获取文件路径
int len=path.LastIndexOf(@"\");
string tempPath=path.Substring(0,len)+"\\"+DateTime.Now.ToString("yyyymmddhhssmm")+tempName;
try
{
File.Copy(path,tempPath);
Response.Write("<script>alert('复制文件成功');</script>");
}
catch(Exception ex)
{
Response.Write(ex.ToString());
}
}private void Button3_Click(object sender, System.EventArgs e)
{//删除文件
string path=Server.MapPath("index4.aspx");
//Response.Write(DateTime.Now.ToString("yyyymmdd"));
//获得文件名
int fileLen=path.LastIndexOf(".");
int pathLen=path.Length;
string tempName=path.Substring(fileLen,pathLen-fileLen);
//获取文件路径
int len=path.LastIndexOf(@"\");
//string tempPath=path.Substring(0,len)+"\\"+DateTime.Now.ToString("yyyymmddhhssmm")+tempName;
string tempPath=path.Substring(0,len)+"\\"+"20063702021337.aspx";
try
{
File.Delete(tempPath);
Response.Write("<script>alert('删除文件成功');</script>");
}
catch(Exception ex)
{
Response.Write(ex.ToString());
}
}
if (di.Exists)
{
//try
//{
di.Delete();
return true;
//} 改为:if (Directory.Exists(path))
{
//try
//{
Directory.Delete(path);
return true;
//}