English 中文(简体)
Get All current file names in directory and write it out in console C#
原标题:

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");
最佳回答

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");
问题回答

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

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.

Try

DirectoryInfo uninstalldir = new DirectoryInfo(Path.GetFullPath("Uninstallers"));




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签