Finding pg_dump and pg_restore (Postgresql) on my C#PC

Asked

Viewed 535 times

2

To perform the backup and Restore in a Postgresql database using C# you need to locate some files like the pg_dump and pg_restore.

How do I make a function so that it is possible to find the files? ie a kind of scan on the computer, to find the Postgresql files

  • Do these files have any extensions? Example: ". exe", ". txt"

  • Yes, they are ". exe"

3 answers

1

If you can run the program by lifting the user (run as administrator) you can simply take the postgres process that is running and look at its path:

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

Obviously you don’t need the loop, just catch one. The Result:

inserir a descrição da imagem aqui

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

On the user upgrade, you can put this next to the application, and when it runs, already request the upgrade.

  • Got it, my doubt is the following, if I run this code on another computer, that in case would not have the pg database and would remote access to my system ... the ps vector would be 0?

  • When I run this function, already with the level of active administrator it returns me the following error Um processo de 32 bits não pode acessar módulos de um processo de 64 bits.

  • if you do not have postgresql on the computer, that is, it is a client, yes, the vector will be returned empty. About the architecture, it is possible that your program is compiling for 32 bits, and is using postgresql 64, or the other way around. Check the properties of your project, build

1

You can use the Directory.GetFiles() combined to a 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 to search the C:\ProgramFiles, but in my opinion this is a little impracticable, choose a more specific place.

  • Right, but how does it work, in my file array at each position I’ll have the path to get to pg_dump and pg_restore? for example in the file array position [0] I would have C: Program Files Postgresql 9.4 bin pg_dump.exe

  • I edited my answer, take a look. @Brunosilva

  • But where do you inform the location to be sought? because at no time can I visualize the C:\ProgramFiles in your code, type, search for all pg_dump.exe filters in C:\ProgramFiles but where did you define it?

  • In that code snippet: Environment.SpecialFolder.ProgramFiles @Brunosilva

  • I just performed its function, and it does not return me the location of the files in the filter :?

  • The files are inside that folder C:\Program Files\PostgreSQL\9.4\bin\ and yet it was not found

  • Only works when I put string Diretorio = @"C:\Program Files\PostgreSQL\9.4\bin\";
 string[] Arquivos = { };
 Arquivos = Directory.GetFiles(Diretorio,"pg_dump.exe"); But then it gets too mailable because I need to put all the way from the kkkkk file

  • @Brunosilva Strange, I don’t really know what the problem is.

  • I’ll try to do a better search, thank you

Show 4 more comments

0


I found a method, which is possible to find pg_dump or other programs, without the need for user upgrade

   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;
    }
  • curiosity: how was the performance of this code ?

  • The first time it runs it takes a while, but the second time is fast ... this code will be adapted and improve, but for now I supply the need.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.