Writing files on Windows Vista

Blitz3D Forums/Blitz3D Beginners Area/Writing files on Windows Vista

PowerPC603(Posted 2007) [#1]
Hi all,

I tried to write a file to the root-dir of C: on my laptop, which has Vista installed, using this code (original filename$ = "C:\Test.dat"):
filename$ = "C:\Program Files\Test.dat"

bnk = CreateBank(256)

For i = 0 To 255
	PokeByte bnk, i, i
Next

file = WriteFile(filename$)

If file <> 0 Then
	WriteBytes bnk, file, 0, 256
Else
	Print "Could not write file"
EndIf
CloseFile file



If file <> 0 Then
	bnk2 = CreateBank(10)
	file2 = ReadFile(filename$)
	ReadBytes bnk2, file2, 0, 10

	For i = 0 To 9
		Print PeekByte(bnk2, i)
	Next
EndIf

CloseFile file2

WaitKey()
End


The code create a bank, fills it with values going from 0 to 255 and then writes the entire bank to a file.
When this is done, some bytes are read from the file (if it was created in the first place) and printed to the screen.

First I tried to write to C:\Test.dat, but this generated the error "Could not write to file" (as set within the code).
Then I modified the Security settings of the entire C-drive so that my main user could write to the disk and then it worked.

But now I have another problem.
As most games' default install-dir is C:\Program Files, I tried writing to this file: C:\Program Files\Test.dat.
It works perfectly, because Blitz writes the file (doesn't return a filehandle of 0) and it can read it's contents as it should.
But when opening the folder, the file is nowhere to be seen (I've set hidden files to display as well).
Even when doing a dir /A from within the command prompt (Start -> Execute -> cmd), the file doesn't seem to exist.
When I write the file to D:\Test.dat, the file shows up and I can open it in Notepad.
But when written to C:\Program Files, Notepad cannot load it, windows doesn't show it, nothing works to open the file, except Blitz itself.
When opening Notepad and opening the file, I go to C:\Program files and type the filename myself (as it doesn't show up in the file-list) and notepad gives me the error that the file doesn't exist.
Why can Blitz create it, and more stupidly, read it when it doesn't exist?

Can someone explain this to me?


JazzieB(Posted 2007) [#2]
It does exist. It's just not been written to where you expected it to be. By default, you are not allowed to write data in the Program Files folder. What happens when a program attempts this is it gets virtualised and written to a hidden folder under the users personal folder. If you look in C:\Users\<username>\AppData\Local\VirtualStore\Program Files you will find your file there.

There are ways around this and this has been discussed many a time on these very forums (have a look in the Win32 forum). The proper solution is not to store additional data that is created by the program in the Program Files folder, but to store it in the Common Application Data folder instead (under Vista this is C:\ProgramData). Again, you will need to set the permissions up correctly, or use the user's own My Documents folder.

If you do some searching you will find all the answers you need. Start in the Win32 forum where it's been discussed a lot.

Basically, Vista is now insisting that applications behave themselves and store data in the correct places. This has always been the "proper" way of doing things, but us programmers have been lazy and just stored everything with the app.


PowerPC603(Posted 2007) [#3]
Oh thanks, I've founds my file there.

But the strange this is that DeleteFile didn't delete the file, it still existed.
But now I got rid of the file, thank you very much.


CodeD(Posted 2007) [#4]
Yeah, Vista is a pain in the butt. You may want to read up on Elevation as well as it's a pertinent issue.

I'm really starting to get sick of Vista's "I think I know what's best for you" attitude.

:P

No, seriously...I am.