我在C:\Documents and Settings\Administrator\桌面\iPOSTBlogSys\userimg\2中的文件夹中有这样一个文件head.jpg
我用下面的一段代码查找此文件 但是怎么也找不到 不知道为什么 哪位大虾能给我的答案
protected void changehead_Click(object sender, EventArgs e)
{
string path=@"userimg\2\head.jpg";
if(File.Exists(path))
{
File.Delete(path);
}
else Response.Write("<script>alert('文件不存在')</script>");
}
我用下面的一段代码查找此文件 但是怎么也找不到 不知道为什么 哪位大虾能给我的答案
protected void changehead_Click(object sender, EventArgs e)
{
string path=@"userimg\2\head.jpg";
if(File.Exists(path))
{
File.Delete(path);
}
else Response.Write("<script>alert('文件不存在')</script>");
}
只能查服务器的文件不能查客户端的文件
另外,好像也只能查虚拟目录下的文件
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;public partial class _Default : System.Web.UI.Page
{
int id = 2;
protected void Page_Load(object sender, EventArgs e)
{
head.ImageUrl = @"userimg\2\head.jpg"; }
protected void Button1_Click(object sender, EventArgs e)
{
string imagename = FileUpload1.FileName;
string type = FileUpload1.FileName.Substring(imagename.LastIndexOf(".")+1).ToLower();
string newname = "head." + type;
DirectoryInfo dir = new DirectoryInfo(Server.MapPath("userimg"));
dir.CreateSubdirectory(id.ToString());
string ipath = Server.MapPath("userimg") + "\\" + id+"\\"+newname;
//string wpath = "userimage" + "\\" + id.ToString() + "\\" + imagename;
if (type == "jpg" || type == "gif" || type == "bmp" || type == "png")
{ FileUpload1.SaveAs(ipath);
}
else
{
Response.Write("<script>alert('格式不正确')</script>");
} }
protected void changehead_Click(object sender, EventArgs e)
{
string path=@"userimg\2\head.jpg";
if(File.Exists(path))
{
File.Delete(path);
}
else Response.Write("<script>alert('文件不存在')</script>");
}
}
button1的功能就是上传一张图片 而button(changehead)的作用就是找到这样图片把他删除 可是为什么可以上传但是不能做到删除功能呢
{
string path=Server.MapPath(@"userimg\2\head.jpg");
if(File.Exists(path))
{
File.Delete(path);
}
else Response.Write(" <script>alert('文件不存在') </script>");
}
你可以看到两个方法调用的路径是不同的,你把changhead的路径改为Server.MapPath(),试试。如果不行的话:再看下文
不知道你的aspx文件中是否有img的html控件,同时但里面的图片不能正常显示。
如果这样,他对之后的你对某个图片的查找也有一定的影响?
一般是这样的。