繁体   English   中英

目录名称无效

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM