我的程序具体是这样的 有三个控件 分别是 FileUpLoad1,Lable1(text属性为空)和Button1
Default.aspx.cs中程序为
using System;
using System.Data;
using System.Configuration;
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;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string str = "E:/ASP.NET/1234/asp.bcp";
int index = 0;
int index1 = 0;
for (int i = str.Length - 1; str[i] != '/'; i--)
{
index1 = i;//index保存文件路径中最后一个'/'的地址
}
index = index1;
string filepath = "";//filepath为读出文件的目录
for (int j = 0; j < index; j++)
{
filepath = filepath + str[j];
}
Label1.Text = filepath;//可读出E:/ASP.NET/1234/
}
}
protected void Button1_Click(object sender, EventArgs e)
{
string str = "E:/ASP.NET/1234/asp.bcp";
int index = 0;
int index1 = 0;
for (int i = str.Length - 1; str[i] != '\\'; i--) {
index1 = i;
}
index = index1;
string filepath = "";//filepath为读出文件的目录
for (int j = 0; j < index; j++)
{
filepath = filepath + str[j];
}
}
}
这样可在Label1中显示E:/ASP.NET/1234/
但是改为
string str = FileUpload1.FileName.ToString();//读出整个文件路径
int index = 0;
int index1 = 0;
for (int i = str.Length - 1; str[i] != '\'; i--) //取一个例如路径为 E:/ASP.NET/1234/asp.bcp 的文件中最后一个'/'的函数
{
index1 = i;//index保存文件路径中最后一个'/'的地址
}
index = index1;
string filepath = "";//filepath为读出文件的目录
for (int j = 0; j < index; j++)
{
filepath = filepath + str[j];
}
Label1.Text = filepath;
后无法运行 因为从FileUpload控件 的brows 中选 则文件后 显示的是路径中为‘\',而不是‘/’