简体   繁体   中英

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

Solution : You need to provide the path of Directory to get all the files in a given Directory based on your search pattern.

From 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" . It should be "C:\\\\DigitalAssets" .

string[] fileNames = Directory.GetFiles(string path) requires a directory, you are giving it a directory + filename.

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() :

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. 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. If you want to enumerate *.eps files, the first argument to GetFiles should be a directory path: @"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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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