Reputation: 1863
I'm writing a program that requires the ability to list all applications installed, and it will do that by listing all the uninstallers for it in a specific directory. This code isn't working, and the directory is created and there are files in the directory
if (startarg.Contains("-il") == true)
{
//Lists all installed programs here
DirectoryInfo uninstalldir = new DirectoryInfo("Uninstallers");
FileInfo[] UninstallerFiles = uninstalldir.GetFiles();
Console.WriteLine("Listing all applications installed with Simtho");
foreach (FileInfo files in UninstallerFiles)
{
Console.WriteLine(files.Name.ToString());
}
I know it needs the full path, but I won't know the full path so it needs to be a variable, how can I make something like this work?
DirectoryInfo uninstalldir = new DirectoryInfo(Directory.GetCurrentDirectory + "\" + "Uninstallers");
Upvotes: 0
Views: 2913
Reputation: 50313
My bet is that for some reason you are passing a wrong path to the DirectoryInfo constructor after all. In order to debug, I would obtain the full path of the dirctory using the following code, and check that it actually refers to the desired path:
Path.Combine(Directory.GetCurrentDirectory(), "Uninstallers");
Upvotes: 2
Reputation: 37514
Try
DirectoryInfo uninstalldir = new DirectoryInfo(Path.GetFullPath("Uninstallers"));
Upvotes: 1
Reputation: 4797
This code seems to be working ,please use this:
class Program
{
static void Main(string[] args)
{
Program p = new Program();
p.getAllFiles(@"D:\Old_Desktop");
}
public void getAllFiles(string directoryPath)
{
DirectoryInfo dirInfo= new DirectoryInfo(directoryPath);
FileInfo[] files= dirInfo.GetFiles();
foreach(FileInfo f in files)
{
Console.WriteLine(f.FullName);
}
Console.ReadLine();
}
}
And yes the directory is to be given as full path as mentioned by earlier answers and in my code snippet.
Upvotes: 1
Reputation: 2201
I believe you "DirectoryInfo" object needs a full path in order to find the uninstallers folder e.g.: "C:...."
Upvotes: 3