Ответ 1
Path.GetFileNameWithoutExtension
Класс Path замечательный.
Я программирую в WPF С#. У меня есть, например, следующий Путь:
C:\Program Files\hello.txt
и я хочу вывести " привет".
Путь - это извлечение строки из базы данных. В настоящее время я использую следующий метод (расщепляется от пути на '\', а затем снова разделяется на '.'):
string path = "C:\\Program Files\\hello.txt";
string[] pathArr = path.Split('\\');
string[] fileArr = pathArr.Last().Split('.');
string fileName = fileArr.Last().ToString();
Это работает, но я считаю, что для этого должно быть более короткое и разумное решение. Любая идея?
Path.GetFileNameWithoutExtension
Класс Path замечательный.
попробовать
fileName = Path.GetFileName (path);
http://msdn.microsoft.com/de-de/library/system.io.path.getfilename.aspx
пытаться
System.IO.Path.GetFileNameWithoutExtension(path);
демонстрация
string fileName = @"C:\mydir\myfile.ext";
string path = @"C:\mydir\";
string result;
result = Path.GetFileNameWithoutExtension(fileName);
Console.WriteLine("GetFileNameWithoutExtension('{0}') returns '{1}'",
fileName, result);
result = Path.GetFileName(path);
Console.WriteLine("GetFileName('{0}') returns '{1}'",
path, result);
// This code produces output similar to the following:
//
// GetFileNameWithoutExtension('C:\mydir\myfile.ext') returns 'myfile'
// GetFileName('C:\mydir\') returns ''
Вы можете использовать Path API следующим образом:
var filenNme = Path.GetFileNameWithoutExtension([File Path]);
Дополнительная информация: Path.GetFileNameWithoutExtension
var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(path);
Попробуйте следующее:
string fileName = Path.GetFileNameWithoutExtension(@"C:\Program Files\hello.txt");
Это вернет "hello" для fileName.
string Location = "C:\\Program Files\\hello.txt";
string FileName = Location.Substring(Location.LastIndexOf('\\') +
1);
Попробуйте это,
string [email protected]"C:\mydir\myfile.ext";
string Result=Path.GetFileName(FilePath);//With Extension
string Result=Path.GetFileNameWithoutExtension(FilePath);//Without Extension
Namespace: using System.IO;
//use this to get file name dynamically
string filelocation = Properties.Settings.Default.Filelocation;
//use this to get file name statically
//string filelocation = @"D:\FileDirectory\";
string[] filesname = Directory.GetFiles(filelocation); //for multiple files
Your path configuration in App.config file if you are going to get file name dynamically -
<userSettings>
<ConsoleApplication13.Properties.Settings>
<setting name="Filelocation" serializeAs="String">
<value>D:\\DeleteFileTest</value>
</setting>
</ConsoleApplication13.Properties.Settings>
</userSettings>
string filepath = "C:\\Program Files\\example.txt";
FileVersionInfo myFileVersionInfo = FileVersionInfo.GetVersionInfo(filepath);
FileInfo fi = new FileInfo(filepath);
Console.WriteLine(fi.Name);
//input to the "fi" is a full path to the file from "filepath"
//This code will return the fileName from the given path
//output
//example.txt