开发者

Creating a file in Delphi with write permission for any user

开发者 https://www.devze.com 2023-01-25 08:01 出处:网络
This might be a dumb question, but I don\'t seem to be able to create a textfile that is writeable by all the u开发者_JS百科sers on a machine, they are always owned by the currently logged in user.

This might be a dumb question, but I don't seem to be able to create a textfile that is writeable by all the u开发者_JS百科sers 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号