locked
How to get exact filename in C# RRS feed

  • Question

  • I have a list of files; i check whether it is present physically and then adds to the zip.

    How do i get exact name of the file that is present physically on the system?

    Thursday, May 23, 2013 1:28 PM

Answers

All replies

  • You can use Path.GetFileName

    string fileName = @"C:\mydir\myfile.ext";
    string path = @"C:\mydir\";
    string result;
    
    result = Path.GetFileName(fileName);
    Console.WriteLine("GetFileName('{0}') returns '{1}'", 
        fileName, result);
    
    result = Path.GetFileName(path);
    Console.WriteLine("GetFileName('{0}') returns '{1}'", 
        path, result);
    
    // This code produces output similar to the following:
    //
    // GetFileName('C:\mydir\myfile.ext') returns 'myfile.ext'
    // GetFileName('C:\mydir\') returns ''


    A.m.a.L Hashim
    Microsoft Most Valuable Professional
    My Blog - Dot Net Goodies

    Thursday, May 23, 2013 1:30 PM
  • try this code :

    Console.WriteLine(Path.GetFileName(@"FilePath"));
                Console.ReadLine();

    Thursday, May 23, 2013 1:45 PM
  • suppose in this case, the file actually present on the system has name MYFILE.ext, does the same code will return result = MYFILE.ext as it is, with the same code posted above?

    Thursday, May 23, 2013 2:01 PM
  • yes , it will
    Thursday, May 23, 2013 2:04 PM
  • I will check and revert it back if needed! Thanks
    Thursday, May 23, 2013 2:05 PM
  • To get the filenames use Directory.GetFiles

    and loop through it/use linq to check the filenames


    Muthukrishnan Ramasamy
    net4.rmkrishnan.net
    Use only what you need, Reduce global warming

    Thursday, May 23, 2013 2:13 PM
  • i used

    result =  Path.GetFileName(fileName);

    but it returned myfile.ext instead of MYFILE.ext

    Thursday, May 23, 2013 2:21 PM
  • File name is not case sensitive. 

    Muthukrishnan Ramasamy
    net4.rmkrishnan.net
    Use only what you need, Reduce global warming

    Thursday, May 23, 2013 2:29 PM
  • what if i want exact file name present physically, is there no way to achieve this functionality?
    Thursday, May 23, 2013 2:38 PM
  • what if i want exact file name present physically, is there no way to achieve this functionality?
    Take a look this discussion http://stackoverflow.com/questions/4763117/how-can-i-obtain-the-case-sensitive-path-on-windows

    A.m.a.L Hashim
    Microsoft Most Valuable Professional
    My Blog - Dot Net Goodies

    Thursday, May 23, 2013 3:38 PM
  • try

    Directory.GetFiles(Path.GetDirectory(filename), Path.GetFileName(filename)



    Muthukrishnan Ramasamy
    net4.rmkrishnan.net
    Use only what you need, Reduce global warming


    Thursday, May 23, 2013 3:49 PM
  • Thanks for the help!

    string s = Directory.GetFiles(Path.GetDirectoryName(FullName), Path.GetFileName(FullName)).FirstOrDefault();

    Although assignment operation dont work here.. right side is correctly returning the string, but failing in assignment.. am i doing anything wrong?

    Friday, May 24, 2013 6:20 AM
  • var directory = new DirectoryInfo(@"C:\");
    foreach (var file in directory.GetFiles())
    {
        MessageBox.Show(file.FullName);
    }


    Teo Selenius

    Friday, May 24, 2013 6:40 AM
  • Thanks Muthukrishnan.. It solved the problem!
    Friday, May 24, 2013 10:10 AM
  • However, i also want the actual path of file as it is. Is there any standard way/API for this?

    Thanks in advance!

    Monday, May 27, 2013 6:17 AM
  • You can use the same functionlity

    Directory.GetFiles(Path.GetDirectory(filename), Path.GetFileName(filename)

    will return full path

    Directory.GetFileName(Directory.GetFiles(Path.GetDirectory(filename), Path.GetFileName(filename))

    will return File name alone


    Muthukrishnan Ramasamy
    net4.rmkrishnan.net
    Use only what you need, Reduce global warming

    Monday, May 27, 2013 5:42 PM