日期:2014-05-19  浏览次数:20675 次

在线求个读取txt文本的问题!谢谢
现在D盘根目录我有个类似这样的txt文本文件“目录.txt”,内容:

C:\Documents   and   Settings\All   Users\Documents
C:\Documents   and   Settings\All   Users\Favorites
C:\Documents   and   Settings\All   Users\ntuser.dat
C:\Documents   and   Settings\All   Users\「开始」菜单
C:\Documents   and   Settings\All   Users\桌面
C:\Documents   and   Settings\All   Users\Application   Data\Adobe
C:\Documents   and   Settings\All   Users\Application   Data\Apple   Computer
C:\Documents   and   Settings\All   Users\Application   Data\Google
C:\Documents   and   Settings\All   Users\Application   Data\hpqwmi
C:\Documents   and   Settings\All   Users\Application   Data\Kaspersky   Anti-Virus   Personal

请问如何做在windowsForm中点击一个按钮后,把每句的最后一个文件名,如第一行的“Documents”;第五行的“桌面”;最后一行的“Kaspersky   Anti-Virus   Personal”等取出来存到Access数据库表1的文件名称字段里(Access表是在D:\目录.mdb)
请高手指点   最好写几句代码   小弟不胜感激

------解决方案--------------------
Split
------解决方案--------------------
不错Split

------解决方案--------------------
str = str.Substring(str.LastIndexOf( "\\ ")+1);
------解决方案--------------------
谢谢回帖
Split是把所有的词变成一个数组 可是我只需要每句最后那个
还有孟子老大您回复的只是把单独一句像 C:\Documents and Settings\All Users\Documents 中的Documents取出来 还请问如何把目录.txt中所有的文件名都给取出来啊
-------
循环读取每一行啊.


------解决方案--------------------
每次读一行数据,读出来后 孟子的办法啊
------解决方案--------------------
从效率上讲,最好是每次读一行,取出最后一个\后的字符串,放入字符串数组,然后一次写入数据库
------解决方案--------------------
FileStream fs = null;
StreamReader sr = null;
try
{
fs = new FileStream( "目录.txt ",FileMode.Open,FileAccess.Read);
sr = new StreamReader(fs,Encoding.Default);
string line = sr.ReadLine();
while(line!=null)
{
int pos = line.LastIndexOf( "\\ ");
if(pos!=-1)
{
string dirName = line.SubString(pos+1);
//插入至库中
}
line = sr.ReadLine();
}
}
finally
{
if(sr!=null)
sr.Close();
if(fs!=null)
fs.Close();
}
------解决方案--------------------
try
{
StreamReader _Reader = new StreamReader( "目录.txt ");
string Line = null;
while ((Line = _Reader.ReadLine()) != null)
{
string Name = Path.GetFileName(Line);
//把Name插入數據庫
}
_Reader.Close();
}
catch (Exception ex)
{

}