views:

182

answers:

4

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");
+3  A: 

I believe you "DirectoryInfo" object needs a full path in order to find the uninstallers folder e.g.: "C:...."

Sam
The thing is, the full path needs to be the current directory, because the path will change because this program is going to be multi-platform
Indebi
Use System.Environment.CurrentDirectory to get a directory object. Using that you can get the files in the current directory and process accordingly.
Sam
+1  A: 

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.

Beginner
+1  A: 

Try

DirectoryInfo uninstalldir = new DirectoryInfo(Path.GetFullPath("Uninstallers"));
luvieere
+2  A: 

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");
Konamiman