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() {
if (new FileInfo(methodsFilename).Exists) {
if (Utils.fileExists(methodsFilename)) {
using (var reader = new BinaryReader(File.Open(methodsFilename, FileMode.Open, FileAccess.Read, FileShare.Read))) {
dumpedMethods = new DumpedMethodsReader(reader).read();
}

View File

@ -72,22 +72,13 @@ namespace de4dot {
static void addIfExists(string basePath, string extraPath) {
try {
var path = Path.Combine(basePath, extraPath);
if (pathExists(path))
if (Utils.pathExists(path))
Instance.addSearchDirectory(path);
}
catch (Exception) {
}
}
static bool pathExists(string path) {
try {
return new DirectoryInfo(path).Exists;
}
catch (Exception) {
return false;
}
}
public void addSearchDirectory(string dir) {
if (!addedDirectories.ContainsKey(dir)) {
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) => {
addSearchDir();
searchDir = new FilesDeobfuscator.SearchDir();
if (!new DirectoryInfo(val).Exists)
if (!Utils.pathExists(val))
exitError(string.Format("Directory {0} does not exist", val));
searchDir.InputDirectory = val;
}));
@ -152,7 +152,7 @@ namespace de4dot {
defaultOption = new OneArgOption("f", null, "Name of .NET file", "file", (val) => {
addFile();
if (!new FileInfo(val).Exists)
if (!Utils.fileExists(val))
exitError(string.Format("File \"{0}\" does not exist.", val));
newFileOptions = new ObfuscatedFile.Options {
Filename = val,
@ -168,7 +168,7 @@ namespace de4dot {
fileOptions.Add(new OneArgOption("m", null, "Name of .methods file", "file", (val) => {
if (newFileOptions == null)
exitError("Missing input file");
if (!new FileInfo(val).Exists)
if (!Utils.fileExists(val))
exitError(string.Format("File \"{0}\" does not exist.", val));
newFileOptions.MethodsFilename = val;
}));

View File

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

View File

@ -198,5 +198,23 @@ namespace de4dot {
return name;
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;
}
}
}
}