Get file name without path

Ant6729

Well-known member
Joined
Jan 22, 2019
Messages
56
Programming Experience
Beginner
Hello everyone!

I need to get only file name wit extension without path

C#:
private void button9_Click(object sender, EventArgs e) 
        {


            string rootFolder = @"C:\Users\Anton\Desktop\?#folder";
            string pattern = @"\b(ArtId=[0-9]*)\b";
            using (StreamWriter sw = File.CreateText(@"C:\Users\Anton\Desktop\?#folder\target.txt"))


     
            foreach (var file in Directory.EnumerateFiles(rootFolder, "*.txt", SearchOption.AllDirectories))


                {


                    using (StreamReader sr = new StreamReader(file, System.Text.Encoding.Default))


                    {
                        string line;
                        while ((line = sr.ReadLine()) != null)




                        {


                            string newstring = line.Substring(0, 8);
                            Match match = Regex.Match(line, pattern, RegexOptions.IgnoreCase);
                            if (match.Success)
                                sw.WriteLine(file + " " + newstring + " " + match.Value);
                            else
                                sw.Write("");


                        }
                    }
                }
            Console.Write("File txt is ready");
            Console.ReadKey();
        }
    }

Could you help me?
 
have you tried FileInfo?
FileInfo finfo = new FileInfo(file)
console.writeline(finfo.Name) # this will spit out just the name of the file excluding the path)
finfo.FullName will give you the full path of the file
 
have you tried FileInfo?
FileInfo finfo = new FileInfo(file)
console.writeline(finfo.Name) # this will spit out just the name of the file excluding the path)
finfo.FullName will give you the full path of the file
You are quite correct but, while a FileInfo is not especially expensive, it is more expensive that just Strings. If you already have a file path and you want just the name, a single call to Path.GetFileName is cheaper and simpler than creating a FileInfo and getting the Name property, if only slightly. I would only create a FileInfo if you wanted to use it for more besides. It's to know about the option though.
 
Another thing to note, although OP here appears to be is working with actual files, it that Path class handles paths in general and doesn't need an accessible file for it to work.
 
Another thing to note, although OP here appears to be is working with actual files, it that Path class handles paths in general and doesn't need an accessible file for it to work.
Strictly speaking, neither does a FileInfo. I just ran this code:
C#:
var fi = new FileInfo(@"Q:\nonexistentfolder\nonexistentfile.ext");

Console.WriteLine(fi.Exists);
and the output was 'False'. There is no Q drive on my machine. You just need a well-formed path, not one that exists.
 
That is logical, since there is an Exists property. I also see now that the value of Name property for that class was initialized with Path.GetFileName.
 
That is logical, since there is an Exists property. I also see now that the value of Name property for that class was initialized with Path.GetFileName.
I expected that that would be the case. I would also imagine that FileInfo.Exists uses File.Exists internally, etc.
 
You can try below code. I think this you need,

C#:
static void Main(string[] args)
{

    var directoryPath = Environment.CurrentDirectory;
    foreach(var fileName in Directory.GetFiles(directoryPath))
    {

        Console.WriteLine(Path.GetFileName(fileName));
    }
    Console.Read();

}
 
Last edited by a moderator:
Back
Top Bottom