Pergunta

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.

Get file name

Foi útil?

Solução

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')
      .....

}

Outras dicas

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]);
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;
                        }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top