Windows Folder Directory

1

I'm developing an application that references local directories on my computer. If I try to run the application on another computer, the references are 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'd like to make directories independent of machines, dynamic, as a way to find the files from a reference point.

    
asked by anonymous 24.06.2016 / 20:24

2 answers

1

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

Alternatively, you can use the environment variable userprofile , as shown in 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, you can always search from the user profile folder logged in.

    
24.06.2016 / 20:53
1

Gabriel, you can use ExtractFilePath(Application.ExeName) to return the current directory that your system is installed on, so 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';
    
24.06.2016 / 21:10