How can I split the path to get the file name 1_Lighthouse_20140306143834816.jpg
? And split the 1_Lighthouse_20140306143834816.jpg
to get the 1
as for my reference that number 1 is already exist.
Asked
Active
Viewed 6,890 times
1

naru
- 129
- 4
- 20
3 Answers
4
Use Path.GetFileName
if(countUser.Length > 0)
{
var file = Path.GetFileName(countUser[0]);
....
and then get the first character from the resulting string using the string indexer
char firstChar = file[0];
if(firstChar == '1')
.....
}

Steve
- 213,761
- 22
- 232
- 286
1
Use Path.GetFileName or Path.GetFileNameWithoutExtension to get the file name. And string.Split to get the first part of the file name.
string filePath = "E:\\folder\1_Lighthouse_XXX.jpg";
var s = Path.GetFileNameWithoutExtension(filePath); //returns without the .jpg
var parts = s.Split(new[] { '_' });
var indexer = Convert.ToInt32(parts[0]);

Sruti
- 670
- 3
- 13
0
for (int i = 0; i < files.Count; i++)
{
//string path = AppDomain.CurrentDomain.BaseDirectory + "Uploads/";
//string filename = Path.GetFileName(Request.Files[i].FileName);
HttpPostedFileBase file = files[i];
//string fname;
// Checking for Internet Explorer
if (Request.Browser.Browser.ToUpper() == "IE" || Request.Browser.Browser.ToUpper() == "INTERNETEXPLORER")
{
string[] testfiles = file.FileName.Split(new char[] { '\\' });
fname = testfiles[testfiles.Length - 1];
}
else
{
fname = file.FileName;
}
}

MS Wani
- 31
- 6
-
Welcome to Stack Overflow! Please don't just throw your source code here. Be nice and try to give a nice description to your answer, so that others will like it and upvote it. See: [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer) – sɐunıɔןɐqɐp Jun 20 '18 at 06:46