简体   繁体   English

目录名称无效

[英]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)需要一个目录,您为其指定了目录+文件名。

MSDN : MSDN

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.

相关问题 Process.Start异常:“目录名称无效” - Process.Start exception: “The directory name is invalid” 目录名称无效,映射的驱动器和用户名 - The Directory Name is Invalid, mapped drives & username Path.GetTempFileName - 目录名无效 - Path.GetTempFileName — Directory name is invalid 目录名称 <Path> 无效| 模仿| Windows服务 - The directory name <Path> is invalid | Impersonation | Windows Service Citrix .net应用程序的目录名称无效 - Citrix .net app with a directory name invalid Win32Exception:目录名无效 - Win32Exception: The directory name is invalid Azure:{“目录路径的语法无效” \\ r \\ n参数名称:initialConfiguration”}错误 - Azure: {“Invalid syntax for directory path ''\r\nParameter name: initialConfiguration”} Error 我删除 zip 文件夹时出现错误,因为目录名称无效 - while im deleting zip folder getting An Error as The directory name is invalid 无效的资源目录名称:“ res animation” aapt.exe - Invalid resource directory name: “res animation” aapt.exe System.Diagnostics.Eventing.Reader.EventLogException:目录名称无效 - System.Diagnostics.Eventing.Reader.EventLogException: The directory name is invalid
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM