I am using the below method to get the file names. But it returns the entire path and I don't want to get the entire path. I want only file names, not the entire path.
How can I get that only file names not the entire path
path= c:\docs\doc\backup-23444444.zip
string[] filenames = Directory.GetFiles(targetdirectory,"backup-*.zip");
foreach (string filename in filenames)
{ }
To get a filename without the path, call the replace() method with the following regular expression: /^. *[\\\/]/ as the first parameter and an empty string as the second. The method will return a string that only contains the filename. Copied!
To extract filename from the file, we use “GetFileName()” method of “Path” class. This method is used to get the file name and extension of the specified path string. The returned value is null if the file path is null. Syntax: public static string GetFileName (string path);
Normally find command will retrieve the filename and its path as one string. If you want to display only the filename, you can use basename command. find infa/bdm/server/source/path -type f -iname "source_fname_*. txt"
You could use the GetFileName method to extract only the filename without a path:
string filenameWithoutPath = Path.GetFileName(filename);
System.IO.Path
is your friend here:
var filenames = from fullFilename
in Directory.EnumerateFiles(targetdirectory,"backup-*.zip")
select Path.GetFileName(fullFilename);
foreach (string filename in filenames)
{
// ...
}
Try GetFileName()
method:
Path.GetFileName(filename);
You can use this, it will give you all file's name without Extension
List<string> lstAllFileName = (from itemFile in dir.GetFiles()
select Path.GetFileNameWithoutExtension(itemFile.FullName)).Cast<string>().ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With