Reputation: 8413
Why am I getting this error? I am using the correct path.
Upvotes: 1
Views: 18302
Reputation: 149
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)
Upvotes: 0
Reputation: 26199
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
}
Upvotes: 7
Reputation: 12956
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.
}
Upvotes: 2
Reputation: 2171
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")
Upvotes: 0
Reputation: 11
the first argument of GetFiles should be only "C:\DigitalAssets" e.FullPath include the file name in it.
Upvotes: 0
Reputation: 3874
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"
Upvotes: 0