【发布时间】:2020-08-13 12:27:15
【问题描述】:
我正在尝试获取我在文件夹中上传的文件的文件路径和文件名。我有这样的路径:
string path = Path.Combine(_webHost.ContentRootPath, "Uploads\\ZipFiles\\");
string extractPath = Path.Combine(_webHost.ContentRootPath, "Uploads\\ExtractedFiles\\");
我将文件上传到path,然后解压缩到extractPath。
string fullPath = Path.GetFullPath(extractPath);
string fileName = Path.GetFileName(extractPath);
fullPath 返回正确的路径,但 fileName 为空。我没有得到文件名。
我正在尝试得到这样的东西
var dbfPath = "C://ExtractedFiles//fileName.jpg";
我打算在一个变量中获取文件路径,在另一个变量中获取文件名,然后将它们连接起来,但我无法获取文件名。最好的方法是什么?
【问题讨论】:
-
从这一行开始: string fileName = Path.GetFileName(extractPath);获取 extractPath 作为输入,并且不包含文件名,它不能返回文件名:-)
-
extractPath在您的示例中是目录的路径,而不是文件。如果要获取该目录中的文件名,可以使用string[] files = Directory.GetFiles(extractPath) -
extractPath 不包含任何文件名,但包含结束目录名称(extractedfiles)。