| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1295 人关注过本帖
标题:请问各位一个有关文件目录读取的问题
只看楼主 加入收藏
xl_1983_10
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2006-9-25
收藏
得分:0 

我的程序具体是这样的 有三个控件 分别是 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 中选 则文件后 显示的是路径中为‘\',而不是‘/’

2006-09-27 14:36
wohemachen
Rank: 1
等 级:新手上路
威 望:2
帖 子:641
专家分:0
注 册:2006-9-21
收藏
得分:0 
我觉得应该这样写:
string str =@ "E:\ASP.NET\1234\asp.bcp";

for (int i = str.Length - 1; str[i] != @"\"; i--)

[此贴子已经被作者于2006-9-27 14:38:28编辑过]


[glow=255,red,2]桃花坞里桃花庵,桃花庵里桃花仙;桃花仙人种桃树,又摘桃花换酒钱。[/glow]
2006-09-27 14:36
xl_1983_10
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2006-9-25
收藏
得分:0 
请问str[i] != @"\";  不对吧  @"\"是string     而str[i] 是char型  怎么比较
2006-09-27 14:39
wohemachen
Rank: 1
等 级:新手上路
威 望:2
帖 子:641
专家分:0
注 册:2006-9-21
收藏
得分:0 
string str =@ "E:\ASP.NET\1234\asp.bcp";

str.Substring(0,str.LastIndexOf(@"\")) ;

就不要这个了“for (int i = str.Length - 1; str[i] != '\'; i--) ”

你觉得这样行吗
(我水平有限,大家一起探讨一下)

[glow=255,red,2]桃花坞里桃花庵,桃花庵里桃花仙;桃花仙人种桃树,又摘桃花换酒钱。[/glow]
2006-09-27 14:54
xl_1983_10
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2006-9-25
收藏
得分:0 

非常感谢大家的帮助
我找到了一个语句,可以直接读出
语句为

if (FileUpload1.HasFile)
Label1.Text = System.IO.Path.GetDirectoryName(FileUpload1.PostedFile.FileName );

执行此语句可以直接读出文件的目录 例如:在FileUpload1中选取文件D:\qq.txt
则Label1中显示D:\

2006-09-29 12:59
快速回复:请问各位一个有关文件目录读取的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.019302 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved