Creating a file in Delphi with write permission for any user - delphi

This might be a dumb question, but I don't seem to be able to create a textfile that is writeable by all the users on a machine, they are always owned by the currently logged in user.
Any ideas? Should I be using TextFile or TFileStream?

I think OP wants to know where to create file writable by all users.
If so, Windows ideology dictates what such files should be created in directory returned by SHGetFolderPath(CSIDL_COMMON_DOCUMENTS) or probabably CSIDL_COMMON_APPDATA

If you create a file it will simply inherit the permissions of its parent container, in other words the folder in which it resides. So you simply need to create it in a folder which has the necessary rights.

What do you mean by "Writable by all the users on a machine" ?
Do you mean you create a file by one user, then another user comes and tries to write into that file, and it fails?
Or do you mean a user creates a file, and other users who are simultaneously connected to the same machine (via terminal sessions) cannot write into the file while the first user is writing in it?
If it is the first case, where is the file saved? Is it in a folder which all users have write access to it?
If it is the second case, you can open the file for read/write without locking it:
var
Stream : TFileStream;
begin
Stream := TFileStream.Create('D:\MyFile',fmOpenReadWrite + fmShareDenyNone);
try
finally
Stream.Free;
end;
end;
Take note with such a code, multiple users might overwrite each other!

Are you talking about NTFS permissions for the file? If yes, you need to look Delphi wrapper for NT security API, and using that API change file security settings to allow Everyone group access the file. If you are just talking about shared access while the file is opened), vcldeveloper above has given the answer.

Related

How to store a signature into the application file?

I want to sign the user application with the user name if he makes a donation. For that I need to write the user name into the exe file. But the file cannot change itself because it is in use at that time. Do you know if anything can be done ? Of course, I can store the signature in a separate file or in registry, but in this way the signature can be lost. I want the modification to be permanent and cannot be changed by the user. I will accept any idea that help me accomplish this...
You can let the application make a copy of itself, then modify the copy, and then start the copy (with ShellExecute).
The modification could be inspired by the UpdateResource examples here: How to attach a resource file to an existing executable file?
The "signature" would be stored in a resource, so your application would have to read it from the resource, for example with Delphi's TResourceStream class.

Keeping record of save place in Delphi

Well, I'm making a program in Delphi that uses a TValueListEditor object to store Keys and Values and save them to a .txt file. I used this functions to do so:
procedure TfrmInserir.FormClose(Sender: TObject; var Action: TCloseAction);
begin
vlePalavras.Strings.SaveToFile('C:\Users\Felipe Knop\Desktop\Felipe\Algoritmos\Delphi\Projetos\Palavras Japonês\Lista.txt');
end;
procedure TfrmInserir.FormCreate(Sender: TObject);
begin
vlePalavras.Strings.LoadFromFile('C:\Users\Felipe Knop\Desktop\Felipe\Algoritmos\Delphi\Projetos\Palavras Japonês\Lista.txt');
end;
In my computer it works fine because the saving path is in the code, but I wanted to share the program with my friends and wanted them to be able to choose the path. Thought about using a TSaveDialog, but since I never used, I don't know if it would do what I want. I need a way to make the user able to choose a path just once and the file will be saved there every time he closes the form. Any ideas?
EDIT: Thank you both Jason and Sean for you answers. Both helped me alot and I figured out a way to make the program more interactive and give my friends the possibility to even share their lists. May sound dumb but it's one of my first programs and I really thank you alot.
Firstly I would suggest not using the FormCreate to load things from file. If its excepts or fails for any reason your form won't load. Create a method called something like "FormInit" and call this after your have created the form, then show it. Allows for better handling of FormInit issues. Same for closing down the form, have a FormDeInit and call it in the "CloseQuery".
Onto your question. I use "ForceDirectories" to ensure that the path chosen exists for saving. This will return false if the directory couldn't be created, true in all other cases. Again you can handle the error nicely when you can't create the folder.
The save dialog is fine for saying where to store the file, however when you run up again how do you know where they set this the last time? Answer you don't, you need to store that somewhere. So the answer here is to simply store your initial configuration in a known place or ask them where it is. I prefer to have base configurations stored in a known place, and store the location of other configs in there. For simplicity lets stay at one level.
If you need to have a known location then use the following calls
ExtractFileDir(Application.ExeName);
This will give you the directory in which the application executable is running. From here you can attach any directory structure you see fit.
If you choose to use the save dialog to get the directory or the application path, make sure to still use the forceDirectory call to make sure the path exists.

Where is the settings stored for ExpressQuantumGrid

I am using Express Quantum Grid from developer express. From code i came to know that they are storing the user settings using TMemIniFile. I know the sections where they are storing but is there any way to open the Ini file and see the contents? If so where is the file located?
From code I came to know that they are storing the user settings using TMemIniFile.
The constructor of TMemIniFile receives the name of the file that stores the INI file. So, you simply need to find the call to TMemIniFile.Create and your answer will be revealed.

How to put configuration information inside the executable?

If we want to store critical information, like passwords and server addresses, inside the executable file generated by the Delphi compiler, how can we do that, without knowing the final executable size and binary structure, like at the end of the file for example?
Side note:
The text to be stored is already encrypted; and in some computers the windows don't give access to write in the registry, specially when the user is not administrator, and there are hacks to monitor registry changes and the smart user can find the new windows registry entry.
Comment
Why this question was down voted? This is achievable! Doesn't meter if not interesting for most people.
I think about the bios and other firmware upgradeable, like satelite tv signal decoders that update themselves. How is that possible?
You can use an .rc file to put your data into a custom resource inside the final .exe file. You can then access that resource at run-time, such as with a TResourceStream, and decrypt and use its content as needed. However, you cannot write new data into the resource while the .exe is running, as the file is locked by the OS. If you need to write new settings, and do not have write access to the Registry, you will have to use a separate file instead. Windows has special folders set aside that users have write access to within their user profiles.
Create a string table resource is one way.
Create a text file. say secretstuff.rc (has to have .rc extension)
with something like this in it.
STRINGTABLE
{
1,"This is my encrypted password in say Base64"
}
Compile it to a .res file with BRCC32.
Include it in the relevant code with a compiler directive
{$R secretstuff.res}
After that you access with TResourceStream.
If you want to manage it a bit better might be wise to stuff them in a dll instead of an exe, then you can update things by delivering a new version of the dll.
There's an example with a it more detail, another purpose but same principle here

Deleting file from C:/ProgramData in Windows 7 does not really delete the file?

I have a little problem with my application. Application stores a SQLite database in C:\ProgramData\ProgramName\ folder on first run. I wanted to test if I delete the database, can application create a new database. On startup, I check if file exists using FileExists(filename) function from SysUtils. But it returns always True, even if I deleted the ProgramName folder.
If I use another folder (for example %AppData%), then it's all OK.
What I'm missing here? Why FileExists function returns always True?
Standard users (and that includes administrators when UAC is active) do not have delete rights to that location.
You need to find a different location to store the file if you need standard users to be able to delete it.

Resources