-2

filepath variable is coming over UDP socket. And after my codes are below:

string filename=null;
string filetype=null;
if (File.Exists(filepath)){
     filename=?????;
     filetype=????;
}

Can I access file name and file type?

crashmstr
  • 28,043
  • 9
  • 61
  • 79
Hasan D.
  • 7
  • 4

3 Answers3

1
var f = new FileInfo(filepath);
filename = f.Name;
filetype = f.Extension;
Tim Lentine
  • 7,782
  • 5
  • 35
  • 40
0
string filename = null;
string filetype = null;
if (File.Exists(filepath)) {
    filename = Path.GetFileName(path);
    filetype = Path.GetExtension(path);
}
Ramie
  • 1,171
  • 2
  • 16
  • 35
0

You can get the filename and filetype using Path class.

filename = Path.GetFileNameWithoutExtension(filepath)
filetype = Path.GetExtension(filepath)
Jayanta Dey
  • 307
  • 1
  • 9