find the name of a csv file in csharp

To find the name of a CSV file in C#, you can use the Path class and its methods. One possible solution is to read in the full file path as a string and then split it using the Path.GetFileName() method:

main.cs
string filePath = "C:\\MyFolder\\MyCsvFile.csv";
string fileName = Path.GetFileName(filePath);
Console.WriteLine($"The file name is {fileName}");
146 chars
4 lines

This will output:

main.cs
The file name is MyCsvFile.csv
31 chars
2 lines

Alternatively, you can use the Path.GetFileNameWithoutExtension() method to get the filename without the extension:

main.cs
string fileWithoutExt = Path.GetFileNameWithoutExtension(filePath);
Console.WriteLine($"The file name without extension is {fileWithoutExt}");
143 chars
3 lines

This will output:

main.cs
The file name without extension is MyCsvFile
45 chars
2 lines

gistlibby LogSnag