Create methods to check whether a file/dir exists

This commit is contained in:
de4dot 2011-11-05 09:56:51 +01:00
parent fe2fe0befe
commit 34a11ee555
5 changed files with 32 additions and 17 deletions

View File

@ -52,7 +52,7 @@ namespace de4dot {
} }
void readMethodsFile() { void readMethodsFile() {
if (new FileInfo(methodsFilename).Exists) { if (Utils.fileExists(methodsFilename)) {
using (var reader = new BinaryReader(File.Open(methodsFilename, FileMode.Open, FileAccess.Read, FileShare.Read))) { using (var reader = new BinaryReader(File.Open(methodsFilename, FileMode.Open, FileAccess.Read, FileShare.Read))) {
dumpedMethods = new DumpedMethodsReader(reader).read(); dumpedMethods = new DumpedMethodsReader(reader).read();
} }

View File

@ -72,22 +72,13 @@ namespace de4dot {
static void addIfExists(string basePath, string extraPath) { static void addIfExists(string basePath, string extraPath) {
try { try {
var path = Path.Combine(basePath, extraPath); var path = Path.Combine(basePath, extraPath);
if (pathExists(path)) if (Utils.pathExists(path))
Instance.addSearchDirectory(path); Instance.addSearchDirectory(path);
} }
catch (Exception) { catch (Exception) {
} }
} }
static bool pathExists(string path) {
try {
return new DirectoryInfo(path).Exists;
}
catch (Exception) {
return false;
}
}
public void addSearchDirectory(string dir) { public void addSearchDirectory(string dir) {
if (!addedDirectories.ContainsKey(dir)) { if (!addedDirectories.ContainsKey(dir)) {
addedDirectories[dir] = true; addedDirectories[dir] = true;

View File

@ -95,7 +95,7 @@ namespace de4dot {
miscOptions.Add(new OneArgOption("r", null, "Scan for .NET files in all subdirs", "dir", (val) => { miscOptions.Add(new OneArgOption("r", null, "Scan for .NET files in all subdirs", "dir", (val) => {
addSearchDir(); addSearchDir();
searchDir = new FilesDeobfuscator.SearchDir(); searchDir = new FilesDeobfuscator.SearchDir();
if (!new DirectoryInfo(val).Exists) if (!Utils.pathExists(val))
exitError(string.Format("Directory {0} does not exist", val)); exitError(string.Format("Directory {0} does not exist", val));
searchDir.InputDirectory = val; searchDir.InputDirectory = val;
})); }));
@ -152,7 +152,7 @@ namespace de4dot {
defaultOption = new OneArgOption("f", null, "Name of .NET file", "file", (val) => { defaultOption = new OneArgOption("f", null, "Name of .NET file", "file", (val) => {
addFile(); addFile();
if (!new FileInfo(val).Exists) if (!Utils.fileExists(val))
exitError(string.Format("File \"{0}\" does not exist.", val)); exitError(string.Format("File \"{0}\" does not exist.", val));
newFileOptions = new ObfuscatedFile.Options { newFileOptions = new ObfuscatedFile.Options {
Filename = val, Filename = val,
@ -168,7 +168,7 @@ namespace de4dot {
fileOptions.Add(new OneArgOption("m", null, "Name of .methods file", "file", (val) => { fileOptions.Add(new OneArgOption("m", null, "Name of .methods file", "file", (val) => {
if (newFileOptions == null) if (newFileOptions == null)
exitError("Missing input file"); exitError("Missing input file");
if (!new FileInfo(val).Exists) if (!Utils.fileExists(val))
exitError(string.Format("File \"{0}\" does not exist.", val)); exitError(string.Format("File \"{0}\" does not exist.", val));
newFileOptions.MethodsFilename = val; newFileOptions.MethodsFilename = val;
})); }));

View File

@ -300,9 +300,15 @@ namespace de4dot {
void createDirectories(string path) { void createDirectories(string path) {
if (string.IsNullOrEmpty(path)) if (string.IsNullOrEmpty(path))
return; return;
var di = new DirectoryInfo(path); try {
if (!di.Exists) var di = new DirectoryInfo(path);
di.Create(); if (!di.Exists)
di.Create();
}
catch (System.Security.SecurityException) {
}
catch (ArgumentException) {
}
} }
} }

View File

@ -198,5 +198,23 @@ namespace de4dot {
return name; return name;
return name.Substring(0, i); return name.Substring(0, i);
} }
public static bool pathExists(string path) {
try {
return new DirectoryInfo(path).Exists;
}
catch (Exception) {
return false;
}
}
public static bool fileExists(string path) {
try {
return new FileInfo(path).Exists;
}
catch (Exception) {
return false;
}
}
} }
} }