Finding pg_dump and pg_restore (PostgreSQL) on my computer with C #

2

To perform backup and restore to a PostgreSQL database using C # you need to find some files like pg_dump and pg_restore .

How do I make a function to be able to find the files? or a kind of scan on the computer, to find PostgreSQL files

    
asked by anonymous 07.09.2017 / 01:12

3 answers

0

I found a method, that it is possible to find pg_dump or other programs, without the necessity of the elevation of user

   private void VerificaPg() {
        CaminhoPg = BuscarArquivo_Dump("pg_dump.exe");
        if (CaminhoPg == String.Empty)
            MessageBox.Show("Postgres não está instalado");
        else
            MessageBox.Show(CaminhoPg);
    }


    private String BuscarArquivo_Dump(String Arquivo)
    {
        String RotaDump = String.Empty;
        try
        {
            DriveInfo[] Drives = DriveInfo.GetDrives();
            foreach (DriveInfo Drive in Drives)
            {
                RotaDump = RealizarBusca(Drive.Name, Arquivo);
                if (RotaDump.Length != 0)
                    break;
            }
        }
        catch (Exception ex)
        { MessageBox.Show(ex.Message); }
        return RotaDump;
    }

    private String RealizarBusca(String DirName, String NomeArchivo)
    {
        try
        {
            if (CaminhoPg.Length == 0)
            {
                try
                {
                    foreach (String Directorio in Directory.GetDirectories(DirName))
                    {
                        System.Security.Permissions.FileIOPermission ReadPermission =
                            new System.Security.Permissions.FileIOPermission(System.Security.Permissions.FileIOPermissionAccess.Write, Directorio);

                        var Permisos = new System.Security.PermissionSet(System.Security.Permissions.PermissionState.None);
                        Permisos.AddPermission(ReadPermission);
                        Boolean Conceder = Permisos.IsSubsetOf(AppDomain.CurrentDomain.PermissionSet);

                        if (Conceder)
                        {
                            try
                            {
                                foreach (String dfile in Directory.GetFiles(Directorio, NomeArchivo))
                                {
                                    CaminhoPg = Directorio + "\";
                                    if (CaminhoPg.Length > 0)
                                    {
                                        Install_Localizado = CaminhoPg;
                                        break;
                                    }
                                }
                                if (CaminhoPg.Length == 0)
                                    RealizarBusca(Directorio, NomeArchivo);
                            }
                            catch (Exception ex)
                            { Console.WriteLine("Não há permissão para continuar", ex.Message.ToString()); }
                        }
                        else { MessageBox.Show("Não há permissão para seguir em frente"); }
                        if (CaminhoPg != string.Empty)
                            break;
                    }
                }
                catch (Exception ex)
                { Console.WriteLine("Não há permissão para seguir em frente", ex.Message.ToString()); }

            }

        }
        catch (Exception ex)
        { Console.WriteLine("Não há permissão para seguir em frente", ex.Message.ToString()); }
        return CaminhoPg;
    }
    
13.09.2017 / 17:58
1

You can use Directory.GetFiles() combined with SearchOption to do this, see how it would look:

string[] arquivos = { };
string[] filtros = { "pg_dum.exe", "pg_restore.exe" };
foreach (string filtro in filtros)
{
    arquivos = arquivos.Concat(Directory.GetFiles(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles), filtro, SearchOption.AllDirectories)).ToArray();
}

In my example I put it to search in C:\ProgramFiles , but in my opinion this is a little impractical, choose a more specific place.

    
07.09.2017 / 01:31
1

If you can run the program by elevating the user (run as administrator) you can simply pick up the postgres process that is running and look at the path of it:

        Process[] ps = Process.GetProcessesByName("postgres"); 
        foreach (Process p in ps)
        {
            FileInfo app = new FileInfo(p.MainModule.FileName);
            string dir = app.Directory.FullName;
        }

Obviously you do not need the loop, just grab one. The Result:

Having the bin folder, you get access to the other postgresql binaries.

On user elevation, you can place this next to the application, and when it is executed, request elevation.

    
08.09.2017 / 04:37