• Hello and welcome to our new forums. We upgraded our forum sites to a more robust and modern system which we hope you will enjoy. Be sure to check out your profile by clicking the button on the top right and configure your preferences, signature, time zone, avatar, etc. as you wish. If you need help with using this new forum'ware try the help link on the bottom right.

    Click here to review your account now.

Get file name without path

Ant6729

Active member
Joined
Jan 22, 2019
Messages
25
Programming Experience
Beginner
Hello everyone!

I need to get only file name wit extension without path

Code:
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?
 

Ant6729

Active member
Joined
Jan 22, 2019
Messages
25
Programming Experience
Beginner
Code:
var name = Path.GetFileName(@"C:\Users\xxxxx.txt");
 

riyanal

New member
Joined
Oct 12, 2011
Messages
2
Programming Experience
1-3
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
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
2,172
Location
Sydney, Australia
Programming Experience
10+
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.
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
482
Location
Norway
Programming Experience
10+
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.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
2,172
Location
Sydney, Australia
Programming Experience
10+
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.
 

JohnH

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
482
Location
Norway
Programming Experience
10+
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.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
2,172
Location
Sydney, Australia
Programming Experience
10+
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.
 

Sagar-Jaybhay

Member
Joined
Feb 19, 2019
Messages
6
Location
Pune
Programming Experience
5-10
Facebook
sagar.jaybhay.906
Twitter
sagar_jaybhay
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:
Top Bottom