如何使用C#获取文件夹值 [英] How to get folder values using C#
本文介绍了如何使用C#获取文件夹值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个名为GPX的文件夹,下面的GPX文件中有值的值
1141370_p1.gpx
1143287_p1.gpx
从上面我想得到 1141370,1143287
。
当我运行我的代码时,在文本值中存储如下:
D:/GPX/DOUFOVOGO \ _1141370_P1.gpxD:/GPX/DOUFOVOGO \ _1143287_P1.gpx
但在文本框中我必须存储的值如 1141370,1143287
为了获得以上输出,我必须做出哪些改变?
我尝试过:
我的代码如下:
protected void btnsubmit_Click( object sender,EventArgs e)
{
foreach (字符串文件在 System.IO.Directory.E中numerateFiles( @ D:/ GPX /, * .gpx))
{
txtgpx.Text + = file + Environment.NewLine;
}
}
解决方案
试试这个:
< pre lang =c#> List< string> files = new List< string>();
foreach (字符串文件 in System.IO.Directory.EnumerateFiles( @ D:/ GPX /, * .gpx))
{
string baseName = Path.GetFileNameWithoutExtension(file);
int index = baseName.LastIndexOf(' _'跨度>);
if (index > 0 )
{
baseName = baseName.Substring( 0 ,index);
}
files.Add(baseName);
}
string allNamesWithCommas = string .Join( ,,files);
i have folder called GPX, in that GPX file below values are there
1141370_p1.gpx 1143287_p1.gpx
From the above i want to get the 1141370,1143287
.
When i run my code, in the text value stored as follows:
D:/GPX/DOUFOVOGO\1141370_P1.gpxD:/GPX/DOUFOVOGO\1143287_P1.gpx
but in the textbox i have to stored values like as 1141370,1143287
For getting above output what changes i have to made?
What I have tried:
my code as follows:
protected void btnsubmit_Click(object sender, EventArgs e)
{
foreach (string file in System.IO.Directory.EnumerateFiles(@"D:/GPX/", "*.gpx"))
{
txtgpx.Text += file + Environment.NewLine;
}
}
解决方案
Try this:
List<string> files = new List<string>(); foreach (string file in System.IO.Directory.EnumerateFiles(@"D:/GPX/", "*.gpx")) { string baseName = Path.GetFileNameWithoutExtension(file); int index = baseName.LastIndexOf('_'); if (index > 0) { baseName = baseName.Substring(0, index); } files.Add(baseName); } string allNamesWithCommas = string.Join(",", files);
这篇关于如何使用C#获取文件夹值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文