[英]The directory name is invalid
Why am I getting this error?为什么会出现此错误? I am using the correct path.我正在使用正确的路径。
Problem : You are providing the Path of File
问题:您正在提供File
路径
Solution : You need to provide the path of Directory
to get all the files in a given Directory
based on your search pattern. 解决方案:您需要提供Directory
的路径,以根据您的搜索模式获取给定Directory
中的所有文件。
From MSDN: Directory.GetFiles() 从MSDN: Directory.GetFiles()
Returns the names of files (including their paths) that match the specified search pattern in the specified directory. 返回与指定目录中的指定搜索模式匹配的文件名(包括其路径)。
Try this: 尝试这个:
string directoryName = Path.GetDirectoryName(e.FullPath);
foreach(String filename in Directory.GetFiles(directoryName,"*.eps"))
{
//your code here
}
You want the directory, not the filename. 您需要目录,而不是文件名。
At the moment, the value of e.FullPath
is "C:\\\\DigitalAssets\\\\LP_10698.eps"
. 目前, e.FullPath
值为"C:\\\\DigitalAssets\\\\LP_10698.eps"
。 It should be "C:\\\\DigitalAssets"
. 它应该是"C:\\\\DigitalAssets"
。
string[] fileNames = Directory.GetFiles(string path)
requires a directory, you are giving it a directory + filename. string[] fileNames = Directory.GetFiles(string path)
需要一个目录,您为其指定了目录+文件名。
Returns the names of files (including their paths) that match the specified search pattern in the specified directory . 返回与指定目录中的指定搜索模式匹配的文件名(包括它们的路径)。
foreach(string filename in Directory.GetFiles(e.FullPath, "*.eps"))
{
// For this to work, e.FullPath needs to be a directory, not a file.
}
You can use Path.GetDirectoryName() : 您可以使用Path.GetDirectoryName() :
foreach(string filename in Directory.GetFiles(Path.GetDirectoryName(e.FullPath), "*.eps"))
{
// Path.GetDirectoryName gets the path as you need
}
You could create a method: 您可以创建一个方法:
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
used to get file names from particular directory. Directory.GetFiles
用于从特定目录获取文件名。 You are trying to get files from file name which is not valid as it gives you error. 您正在尝试从无效的文件名中获取文件,因为它会给您错误。 Provide directory name to a function instead of file name. 为函数提供目录名,而不是文件名。
You can try 你可以试试
Directory.GetFiles(System.IO.Path.GetDirectoryName(e.FullPath),"*.eps")
e.FullPath
seems to be a file, not a directory. e.FullPath
似乎是一个文件,而不是目录。 If you want to enumerate *.eps files, the first argument to GetFiles should be a directory path: @"C:\\DigitalAssets"
如果要枚举* .eps文件,则GetFiles的第一个参数应为目录路径: @"C:\\DigitalAssets"
GetFiles的第一个参数应仅为“ C:\\ DigitalAssets”。e.FullPath在其中包含文件名。
For deleting files from a directory用于从目录中删除文件
var files = Directory.GetFiles(directory)
foreach(var file in files)
{
File.Delete(file);
}
In short to delete the file use总之删除文件使用
File.Delete(filePath);
and to delete the directory并删除目录
Directory.Delete(directoryPath)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.