0

how to copy the same string and ignore the file directory path?

Like Below:

  "C:\\Desktop\\Username\\filename\\filename"
  "D:\\Username\\filename\\filename\\filename"
  "E:\\Filename\\filename\\filename\\filename"

The Example:

string file = dialog.FileName;
string getfile = file;

if(file.Contains("Dangerous"))  // Check the file Contains these word
{
    string getfilename = file.Substring(3);   //with this only available ignore C:\\ or D:\\
}

Output:

Dangerous_2018_09_10_1.csv

Please share any clue with me, Thanks.

Comsphere
  • 137
  • 2
  • 11

1 Answers1

2

Updated after the comment of @PanagiotisKanavos. This is probably the best way.

var fileName = File.GetFileName(file);

It could also be achieved this way, but use above if your only intention is to get the filename.

var myFileInfo = new FileInfo(file);
var fileName = myFileInfo.Name;
Sander Declerck
  • 2,455
  • 4
  • 28
  • 38