Delphi: file access and compatibility files (windows 7)

Suppose my application in the name mysoft and the file I want to read is afile.bin . mysoft is located in the folder with the program file. Assume also that mysoft is not running as an administrator.

 procedure TForm1.Button1Click(Sender: TObject); var afile: File of Byte; begin AssignFile(Infile, 'C:\Program Files\mysoft\afile.bin'); Reset(afile); CloseFile(afile); end; 

The simple code above will create a copy of afile.bin in the folder with the compatibility files.

My questions:

  • Why? I only read the damn file, I don't want to change it.
  • How to prevent this, in addition to moving the file to another folder?

Again I only read the data file, I do not understand it.

+4
source share
3 answers

Calling Reset opens the file for writing. This is because the default value of System.FileMode is fmOpenReadWrite . Since the application is virtualized, and since your user token does not have administrator rights, the virtualization level starts and creates a copy of the file in virtual storage.

A solution available using legacy Pascal I / O is to set FileMode to fmOpenRead . Reset will then open the file for reading, and the virtualization layer will not be called.

Remember that FileMode is a global variable that affects all obsolete I / O Pascal. Woe, if you have streams, or forget to set FileMode to fmOpenReadWrite when you need to write a file. Rebellion right? Global global process flags for controlling file access mode! Well, this stuff was invented long before applications had threads. And long before OOP was invented. Therefore, I can understand why this is so.

The lesson that needs to be removed is that it's time to move on to one of the modern forms of file access. For example, TFileStream .

In fact, this is also the time when you turned off virtualization in your application. Virtualization was introduced in Vista almost 6 years ago. This was intended to help older programs that have not yet been recompiled for Vista. Your application should not use it.

+5
source

If you have a version of Delphi that supports the TStreamReader class, and your file is text, you can use this code:

 procedure TForm1.btn1Click(Sender: TObject); var SR : TStreamReader; line : string; begin SR := TStreamReader.Create('C:\Program Files\mysoft\afile.bin'); try while not SR.EndOfStream do begin line := SR.ReadLine; end; finally SR.Free; end; end; 

If not, I suggest a more classic approach that works in any file:

 procedure TForm1.btn2Click(Sender: TObject); var FS : TFileStream; mem : array of byte; begin FS := TFileStream.Create('C:\Program Files\mysoft\afile.bin',fmOpenRead or fmShareDenyNone); try SetLength(mem,FS.Size); FS.Read(mem[0],FS.Size); finally FS.Free; end; end; 
+2
source

I believe this is due to the fact that the new Windows 7 feature, which prevents you from making direct access to the file in the file system area (% programfiles%,% systemroot%,% systemdrive% or% programdata%).

Try changing the code to open the file using TFileStream , and set Mode to fmOpenRead .

I hope this works. I don't have 7 to try :)

0
source

Source: https://habr.com/ru/post/1438960/


All Articles