[英]The directory name is invalid
為什么會出現此錯誤? 我正在使用正確的路徑。
問題:您正在提供File
路徑
解決方案:您需要提供Directory
的路徑,以根據您的搜索模式獲取給定Directory
中的所有文件。
從MSDN: Directory.GetFiles()
返回與指定目錄中的指定搜索模式匹配的文件名(包括其路徑)。
嘗試這個:
string directoryName = Path.GetDirectoryName(e.FullPath);
foreach(String filename in Directory.GetFiles(directoryName,"*.eps"))
{
//your code here
}
您需要目錄,而不是文件名。
目前, e.FullPath
值為"C:\\\\DigitalAssets\\\\LP_10698.eps"
。 它應該是"C:\\\\DigitalAssets"
。
string[] fileNames = Directory.GetFiles(string path)
需要一個目錄,您為其指定了目錄+文件名。
MSDN :
返回與指定目錄中的指定搜索模式匹配的文件名(包括它們的路徑)。
foreach(string filename in Directory.GetFiles(e.FullPath, "*.eps"))
{
// For this to work, e.FullPath needs to be a directory, not a file.
}
您可以使用Path.GetDirectoryName() :
foreach(string filename in Directory.GetFiles(Path.GetDirectoryName(e.FullPath), "*.eps"))
{
// Path.GetDirectoryName gets the path as you need
}
您可以創建一個方法:
public string GetFilesInSameFolderAs(string filename)
{
return Directory.GetFiles(Path.GetDirectoryName(filename), Path.GetExtension(filename));
}
foreach(string filename in GetFilesInSameFolderAs(e.FullPath))
{
// do something with files.
}
Directory.GetFiles
用於從特定目錄獲取文件名。 您正在嘗試從無效的文件名中獲取文件,因為它會給您錯誤。 為函數提供目錄名,而不是文件名。
你可以試試
Directory.GetFiles(System.IO.Path.GetDirectoryName(e.FullPath),"*.eps")
e.FullPath
似乎是一個文件,而不是目錄。 如果要枚舉* .eps文件,則GetFiles的第一個參數應為目錄路徑: @"C:\\DigitalAssets"
GetFiles的第一個參數應僅為“ C:\\ DigitalAssets”。e.FullPath在其中包含文件名。
用於從目錄中刪除文件
var files = Directory.GetFiles(directory)
foreach(var file in files)
{
File.Delete(file);
}
總之刪除文件使用
File.Delete(filePath);
並刪除目錄
Directory.Delete(directoryPath)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.