how to search if a file is present in the computer in c sharp?i need to find out if pdf reader is installed or not in a pc...please help..thank you

Dani AI

Generated

and are on the right path: use the registry, not a file system crawl. Two gotchas to make this reliable today are 32/64-bit registry views and per-user installs. Do not hardcode Wow6432Node; open the hive with RegistryView and check both HKLM and HKCU so you catch machine-wide and per-user installs. Then prefer App Paths to get the actual AcroRd32.exe path; installers are expected to register there. If you only need "is any PDF viewer available," consider checking the default association instead of looking for Adobe specifically. Registry keys affected by WOW64. App Paths registration.

using System.IO;
using Microsoft.Win32;

static bool TryFindAcrobatReader(out string exePath)
{
    exePath = null;
    foreach (var view in new[] { RegistryView.Registry64, RegistryView.Registry32 })
    foreach (var hive in new[] { RegistryHive.LocalMachine, RegistryHive.CurrentUser })
    {
        using var root = RegistryKey.OpenBaseKey(hive, view);

        using (var appPath = root.OpenSubKey(
            @"SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths\AcroRd32.exe"))
        {
            var p = appPath?.GetValue("") as string;
            if (!string.IsNullOrEmpty(p) && File.Exists(p)) { exePath = p; return true; }
        }

        using (var reader = root.OpenSubKey(@"SOFTWARE\Adobe\Acrobat Reader"))
        {
            var versions = reader?.GetSubKeyNames();
            if (versions != null && versions.Length > 0)
            {
                var dir = reader.OpenSubKey(versions[0])?.GetValue("InstallPath") as string;
                var p = Path.Combine(dir ?? "", "AcroRd32.exe");
                if (File.Exists(p)) { exePath = p; return true; }
            }
        }
    }
    return false;
}

On ’s point: HKCR.pdf\OpenWithList shows candidates, not the default. On Windows 8+ the canonical default lives under HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts.pdf\UserChoice (ProgId with a protected hash), and apps should not set it directly anymore; prompt the user or link them to Default apps. Also remember HKCR is a merged view of HKCU\Software\Classes and HKLM\Software\Classes, so read both when you need authoritative data. Many PCs will have a PDF handler without Adobe (for example, Microsoft Edge’s built-in viewer). File type and protocol associations model. HKEY_CLASSES_ROOT key. Edge PDF viewer policy.

Recommended Answers

All 4 Replies

I'd check the registry rather than try to search the 100,000's of files on someones computer. On my machine, the key is Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Adobe\Acrobat Reader (this is W7 Ultimate 64bit). You'll probably have to find OS versions to figure out where it is for them.

Registry Class

Try to use this code:

using System;
using Microsoft.Win32;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
            RegistryKey adobe = Registry.LocalMachine.OpenSubKey("Software").OpenSubKey("Adobe");
            if (adobe != null)
            {
                RegistryKey acroRead = adobe.OpenSubKey("Acrobat Reader");
                if (acroRead != null)
                {
                    string[] acroReadVersions = acroRead.GetSubKeyNames();
                    Console.WriteLine("The following version(s) of Acrobat Reader are installed: ");
                    foreach (string versionNumber in acroReadVersions)
                    {
                        Console.WriteLine(versionNumber);
                    }
                }
            }
        }
    }
}

For searching any PDF reader, I would rather check for extensions in registers. Here you would get also which application takes care of pdf files. By default Windows doesn't know this extension, so this can help to check whether there is or not any PDF reader.
For example check here:
HKEY_CLASSES_ROOT\.pdf\OpenWithList

(Windows 7/8 can have built-in PDF reader, have not checked it )

thanks a lot everyone...thanks mitja..it worked :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.