cosito Asked: 2020-05-09 07:13:43 +0800 CST 2020-05-09 07:13:43 +0800 CST 2020-05-09 07:13:43 +0800 CST Get name of file being read 772 How can I save the name of the file that is being read and save it in a variable? string[] lineas = System.IO.File.ReadAllLines(@"XXXXX"); c# 3 Answers Voted jachguate 2020-05-09T07:19:09+08:002020-05-09T07:19:09+08:00 First assign the file name to a string variable and then use that variable to open the file Something like that: string nombreArvhivo = @"XXXXX" string[] lineas = System.IO.File.ReadAllLines(nombreArchivo); //nombreArchivo tiene el nombre del archivo leído. Best Answer Marc 2020-05-09T07:20:24+08:002020-05-09T07:20:24+08:00 If you want to get only the file name you can do the following: string nombreArchivo = System.IO.Path.GetFileName(@"XXXXX"); This way you get just the file name, without the full path. Juan Salvador Portugal 2020-05-09T07:23:31+08:002020-05-09T07:23:31+08:00 To obtain the name of a file knowing the path, you can do it with Path.GetFileName(string)if you are interested in keeping the extension, or Path.GetFileNameWithoutExtension(string)in case you prefer the file name without it. Therefore, it would be enough to store the path in a variable and make use of one of these methods. string path = "@XXXX"; string[] lineas = System.IO.File.ReadAllLines(path); string nombreArchivoConExtension = System.IO.Path.GetFileName(path); string nombreArchivoSinExtension = System.IO.Path.GetFileNameWithoutExtension(path); Assuming it path was worthC:\Info\CATÁLOGOS\ANILAG P1T M12.pdf the variable nombreArchivoConExtensionwill be equal to ANILAG P1T M12.pdfandnombreArchivoSinExtension ANILAG P1T M12
First assign the file name to a string variable and then use that variable to open the file
Something like that:
If you want to get only the file name you can do the following:
This way you get just the file name, without the full path.
To obtain the name of a file knowing the path, you can do it with
Path.GetFileName(string)
if you are interested in keeping the extension, orPath.GetFileNameWithoutExtension(string)
in case you prefer the file name without it.Therefore, it would be enough to store the path in a variable and make use of one of these methods.
Assuming it
path
was worthC:\Info\CATÁLOGOS\ANILAG P1T M12.pdf
the variable
nombreArchivoConExtension
will be equal toANILAG P1T M12.pdf
andnombreArchivoSinExtension
ANILAG P1T M12