从目录中获取文件路径和文件名 - C#
Get file path and file name from directory - C#
我正在尝试获取我上传到文件夹中的文件的文件路径和文件名。我有这样的路径:
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
returns 正确的路径但是 fileName
是空的。我没有得到文件名。
我想得到这样的东西
var dbfPath = "C://ExtractedFiles//fileName.jpg";
我打算在一个变量中获取文件路径,在另一个变量中获取文件名,然后将它们连接起来,但我无法获取文件名。执行此操作的最佳方法是什么?
获取 ExtractedFiles 文件夹中的文件:
string[] files = Directory.GetFiles(extractPath)
如果您的 zip 文件中有文件夹,并且您想递归地获取其中的所有文件,请使用:
string[] files = Directory.GetFiles(extractPath, "*.*", SearchOption.AllDirectories)
我正在尝试获取我上传到文件夹中的文件的文件路径和文件名。我有这样的路径:
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
returns 正确的路径但是 fileName
是空的。我没有得到文件名。
我想得到这样的东西
var dbfPath = "C://ExtractedFiles//fileName.jpg";
我打算在一个变量中获取文件路径,在另一个变量中获取文件名,然后将它们连接起来,但我无法获取文件名。执行此操作的最佳方法是什么?
获取 ExtractedFiles 文件夹中的文件:
string[] files = Directory.GetFiles(extractPath)
如果您的 zip 文件中有文件夹,并且您想递归地获取其中的所有文件,请使用:
string[] files = Directory.GetFiles(extractPath, "*.*", SearchOption.AllDirectories)