[Solved] Checking if a folder is write protected lazarus / 21-General ---------------------------------------------------------------------- petevick · 2022-04-18 10:59 I'm using DirectoryIsWritable(CopyPath) to check if a folder can be written to, if it is then a folder gets written to it. I use it in two different Windows 10 projects, and I'm checking the same folder in each project, the folder resides in C:\Program Files\. One project correctly detects the folder is not writable, in the other project it says it is writable and copies the folder to C:\Users\\AppData\Local\VirtualStore\Program Files\. So why is the identical function using the identical variable giving two different results ---------------------------------------------------------------------- Handoko · 2022-04-18 12:00 I'm not sure but I think maybe UAC on vs off: https://www.tenforums.com/general-support/191354-what- virtualstore.html ---------------------------------------------------------------------- BobDog · 2022-04-18 12:33 You could check with this. Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  program attributes; uses process;  function getattributes(FolderOrFilename:ans istring):boolean; var s,chr,g:ansistring; i,flag:integer; ans:boolean= false; begin flag:=0;  RunCommand('attrib ',['/d',FolderOrFilename],s);  writeln('___________');  writeln('Output from attrib = ',s);   if s[1]='F' then exit(false);  writeln('Meaning (if applicable):');  for i:=1 to 20 do  begin  chr:=s[i];  case chr of  'A':begin writeln('archive');flag:=0; end;  'C':begin writeln('compressed');flag:=0; end;  'H':begin writeln('hidden');flag:=0; end;  'I':begin writeln('not content indexed');flag:=0; end;  'O':begin writeln('offline');flag:=0; end;  'P':begin writeln('pinned (Windows 10 and OneDrive only)');flag:=0; end;  'R':begin writeln('readonly');flag:=1; end;  'S':begin writeln('system');flag:=1; end;  'T':begin writeln('temperory');flag:=0; end;  'U':begin writeln('unpinned (Windows 10 and OneDrive only)');flag:=0; end;  'V':begin writeln('integrity (Windows Server 2012R2+ ReFS only0');flag:=0; end;  'X':begin writeln('no_scrub_data (Windows Server 2012R2+ ReFS only)');flag:=0; end;  end;  end;  exit (flag=0); end;   begin  writeln(getattributes('C:\ProgramData'),' is writable');  writeln(getattributes('C:\Program Files\WindowsApps'),' is writable');  writeln(getattributes('C:\windows\system32'),' is writable');  writeln(getattributes('C:\Users\Computer\Desktop\test'),' is writable');  writeln(getattributes('C:\Users\Computer\Documents'),' is writable');  writeln('Press return to end');  readln;  end. ---------------------------------------------------------------------- petevick · 2022-04-18 18:25 Quote from: Handoko on April 18, 2022, 02:00:52 pmI'm not sure but I think maybe UAC on vs off: https://www.tenforums.com/general-support/191354-what- virtualstore.html Nope, it's the same Windows session used for each project. ---------------------------------------------------------------------- jamie · 2022-04-18 22:14 That function is very poorly written, I mean VERY poorly written. I would never use it. ---------------------------------------------------------------------- BobDog · 2022-04-18 22:51 Win 10 Googling around I note that the only read only  folder in Windows is "documents" The read only box under other folder properties is filled in (like documents) but cannot be changed to read only, windows ignores changes you try to make. I have tried with the attrib command with +R, but it doesn't apply to folders, only files. Reminder-tested win 10 only. ---------------------------------------------------------------------- Remy Lebeau · 2022-04-18 22:56 Quote from: petevick on April 18, 2022, 12:59:03 pmI'm using DirectoryIsWritable(CopyPath) to check if a folder can be written to, if it is then a folder gets written to it. That is a waste of time.  Either you have write access, or you don't.  Just write what you need, when you need it, and catch any error that may occur.  Don't bother asking the OS first, as the answer might have changed by the time you actually try to write. Quote from: petevick on April 18, 2022, 12:59:03 pmSo why is the identical function using the identical variable giving two different results  Per MSDN: New UAC Technologies for Windows Vista QuotePrior to Windows Vista, many applications were typically run by administrators. As a result, applications could freely read and write system files and registry keys. If standard users ran these applications, they would fail due to insufficient access. Windows Vista improves application compatibility for standard users by redirecting writes (and subsequent file or registry operations) to a per-user location within the user’s profile. For example, if an application attempts to write to C:\Program Files\Contoso\Settings.ini, and the user does not have permissions to write to that directory, the write will get redirected to C:\Users\Username\AppData\Local\VirtualStore\Program Files\contoso\settings.ini. For the registry, if an application attempts to write to HKEY_LOCAL_MACHINE\Software\Contoso\ it will automatically get redirected to HKEY_CURRENT_USER\Software\Classes\Vir tualStore\MACHINE\Software\Contoso or HKEY_USERS\UserSID_Classes\VirtualStore\Machine\Software\Contoso. ... Virtualization is only enabled for: 32 bit interactive processesAdministrator writeable file/folder and registry keys Virtualization is disabled for: 64 bit processesNon-interactive processesProcesses that impersonateKernel mode callersExecutables that have a requestedExecutionLevel Virtualization and roaming: Virtualization files/folder and registry keys do not roamAssociated with global objects that do not roam ... File virtualization addresses the situation where an application relies on the ability to store a file, such as a configuration file, in a system location typically writeable only by administrators. Running programs as a standard user in this situation might result in program failures due to insufficient levels of access. When an application writes to a system location only writeable by administrators, Windows then writes all subsequent file operations to a user-specific path under the Virtual Store directory, which is located at %LOCALAPPDATA%\VirtualStore. Later, when the application reads back this file, the computer will provide the one in the Virtual Store. Because the Windows security infrastructure processes the virtualization without the application’s assistance, the application believes it was able to successfully read and write directly to Program Files. The transparency of file virtualization enables applications to perceive that they are writing and reading from the protected resource, when in fact they are accessing the virtualized version. ... So, the most likely difference between the two apps is that: one app is likely a 32bit process without a UAC manifest, thus gets virtualized to VirtualStore.the other app is likely either a 64bit process, or a 32bit process with a UAC manifest, thus is not virtualized to VirtualStore. ---------------------------------------------------------------------- petevick · 2022-04-19 05:17 Quote from: jamie on April 19, 2022, 12:14:40 amThat function is very poorly written, I mean VERY poorly written. I would never use it. well that's great, so what would you use ?? ---------------------------------------------------------------------- petevick · 2022-04-19 05:26 Quote from: Remy Lebeau on April 19, 2022, 12:56:06 amone app is likely a 32bit process without a UAC manifest, thus gets virtualized to VirtualStore.the other app is likely either a 64bit process, or a 32bit process with a UAC manifest, thus is not virtualized to VirtualStore.Nail on the head, I'd unchecked the Windows manifest option  in the problem project. It's now working as expected. Thanks Remy for the post.