Directory of Windows folders

Asked

Viewed 865 times

1

I am developing an application that references local directories on my computer. If I try to run the application on another computer, the references get lost.

nmArquivo := 'C:\Users\Admin\Desktop\Protetor de Tela\Img.txt';
nmArquivoImgAtual := 'C:\Users\Admin\Desktop\Protetor de Tela\ImgAtual.txt';
MinutoAtual := 'C:\Users\Admin\Desktop\Protetor de Tela\MinutoAtual.txt';
HoraAtual := 'C:\Users\Admin\Desktop\Protetor de Tela\HoraAtual.txt';

Well I would like to make machine-independent directories dynamic as a way to find the files from a reference point.

  • tries to put in C: its progama.. will be standard for all computers

2 answers

1

Gabriel, not every person has a user Admin, usually people have in your Windows a user with your name, so at the time you set the path C:\Users\Admin\Desktop\Protetor de Tela\ will only work for those who have logged in as user Admin.

An alternative is you use the environment variable userprofile, as I show from the example

    userProfile := GetEnvironmentVariable('userprofile');
    nmArquivo := userProfile +'\Desktop\Protetor de Tela\Img.txt';
    nmArquivoImgAtual := userProfile + '\Desktop\Protetor de Tela\ImgAtual.txt';
    MinutoAtual := userProfile + '\Desktop\Protetor de Tela\MinutoAtual.txt';
    HoraAtual := userProfile + '\Desktop\Protetor de Tela\HoraAtual.txt';

With this approach your system is independent of logged in user, not needing to fix a single path, can always search from the profile folder of the logged in user.

1

Gabriel, you can use the ExtractFilePath(Application.ExeName) to return the current directory your system is installed in, this way it will work for any computer you use.

Example:

mApplicationPath := ExtractFilePath(Application.ExeName);

nmArquivo := mApplicationPath + 'Img.txt';
nmArquivoImgAtual := mApplicationPath + 'ImgAtual.txt';
MinutoAtual := mApplicationPath + 'MinutoAtual.txt';
HoraAtual := mApplicationPath + 'HoraAtual.txt';

Browser other questions tagged

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