Page 1 of 1

Saving file problem

Posted: Sat Oct 11, 2014 12:38 pm
by doctorized
I have the following code to write the elements of a listgadget to a file.
If the path is "C:\" the file is not created (Win 7 x64 Ultimate) but OpenFile() returns nonzero.
If FileSize() is called in the end, nonzero is returned but the file is nowhere. What do I do wrong?
I know that the OS is not giving me permission to write on "C:\" but why PB 5.30 is telling me that the file is writen successfully?
How can I find out if the file is really writen?

Code: Select all

Procedure SaveFile()
Pattern$ = "Text (*.txt)|*.txt|All files (*.*)|*.*"
Pattern = 0
File$ = SaveFileRequester("Please choose file name", "LAN Info", Pattern$, Pattern)
If File$
	If ReadFile(0,File$);file already exists
		CloseFile(0)
		i=MessageRequester("Warning","File already exists. Overwrite ?",#PB_MessageRequester_YesNo)
		If i= #PB_MessageRequester_No			
			ProcedureReturn
		EndIf
	EndIf
	If GetExtensionPart(File$) = ""
		File$ + ".txt"
	EndIf
	ii = OpenFile(#PB_Any,File$)
	If ii
		For i=0 To CountGadgetItems(0)
			tmp$ = GetGadgetItemText(0,i,0)
			If tmp$ = ""
				WriteStringN(ii, "")
			ElseIf FindString(tmp$,"Connection  #") > 0
				WriteStringN(ii,tmp$)
			Else
				WriteStringN(ii,LSet(tmp$,36," ") + " : " + GetGadgetItemText(0,i,1))
			EndIf
		Next
		CloseFile(ii)
		MessageRequester("","File created successfully.")
	Else
		MessageRequester("","Cannot create file.",#MB_ICONERROR)
	EndIf
EndIf
EndProcedure

Re: Saving file problem

Posted: Sat Oct 11, 2014 1:08 pm
by deesko
Can you try giving your program administrator priveligies and see if you get different results?

Re: Saving file problem

Posted: Sat Oct 11, 2014 1:34 pm
by Bisonte
Compile it with "User" privilegs, and see if it works...

Normally if you have adminprivilegs, you can write all over, and in "normal" mode, UAC simulate privilegs... (Virtualization).
So the User - Mode have in the result the right value.

Re: Saving file problem

Posted: Sat Oct 11, 2014 1:44 pm
by doctorized
Turning user mode on, after pressing the "save" button, a window pops up telling me that I do not have rights to write here.
Turning admin rights on the file is created successfully. This annoys me a little as I have to use admin rights in order to be sure that the code is working as it should be. OpenFile() should inform me that the file cannot be opened. FileSize() should not return a nonzero value as no data are really writen. Is it a bug or I think so?

Re: Saving file problem

Posted: Sat Oct 11, 2014 2:08 pm
by deesko
Is the filesize value the same even if admin is turned off (besides being nonzero)?

Re: Saving file problem

Posted: Sat Oct 11, 2014 2:12 pm
by RASHAD
Search "C:\Users\???(the user name)\AppData\Local\VirtualStore" to see if your file is there or not
Probably you need to enable seeing the hidden files and directory for Explorer to be able to see "AppData"

Re: Saving file problem

Posted: Sat Oct 11, 2014 2:59 pm
by doctorized
deesko wrote:Is the filesize value the same even if admin is turned off (besides being nonzero)?
Yes, it is the same.
RASHAD wrote:Search "C:\Users\???(the user name)\AppData\Local\VirtualStore" to see if your file is there or not
Probably you need to enable seeing the hidden files and directory for Explorer to be able to see "AppData"
Yes, the file is in this folder when admin rights are off. But i still insist, is it a bug or not as I check file size of a file in "C:\" and not in VirtualStore.

Re: Saving file problem

Posted: Sat Oct 11, 2014 3:25 pm
by RASHAD
PB checks about the exciting of the file through the Operating System
Is it there ? Yes
Due to dealing with 3 OS it will be extra work to check thoroughly by itself
So you can report to the request forum maybe :)

Re: Saving file problem

Posted: Sun Oct 12, 2014 9:11 am
by doctorized
RASHAD wrote:PB checks about the exciting of the file through the Operating System
Is it there ? Yes
Help me understand one thing please. You mean that PB is looking for the file all over the disk?
When I give a file in FileSize() is the procedure looking for the file in many locations other than the path I gave in the case the file is not there?

Re: Saving file problem

Posted: Sun Oct 12, 2014 10:01 am
by PB
> You mean that PB is looking for the file all over the disk?

No, it doesn't. It only looks where you specify.

Re: Saving file problem

Posted: Sun Oct 12, 2014 10:22 am
by HeX0R
Ticking User Mode should be the correct method, this will inform your program, when it doesn't have writing rights and the user should select another path.
Unticking user mode, will create a not-windows7-compatible executable, so windows thinks it is an old program meant to be running under win xp.
Therefore windows simulates a writing permission to folders, where in fact it hasn't any (and will store it in the virtual store).

In this mode, you can still use openfile, readfile, createfile, filesize, [...] because windows will automatically redirect.
So all of your file actions will still work, but the file is somewhere else (but who cares?).

Usually you should select user mode by default.

Re: Saving file problem

Posted: Sun Oct 12, 2014 1:01 pm
by doctorized
HeX0R wrote:So all of your file actions will still work, but the file is somewhere else (but who cares?).
I care and the users will care if they save a file using a program of mine and when they try to find the file to open it or send it and the file will be absent!

Re: Saving file problem

Posted: Mon Oct 13, 2014 5:48 am
by citystate
I think the problem is just location, you should be able to save somewhere like GetHomeDirectory() or GetTemporaryDirectory(), just not in C:\

of course, that doesn't address the issue of not getting a 0 result during file creation...

Re: Saving file problem

Posted: Mon Oct 13, 2014 1:31 pm
by doctorized
citystate wrote:I think the problem is just location, you should be able to save somewhere like GetHomeDirectory() or GetTemporaryDirectory(), just not in C:\
It is not just me, it is what path will someone choose to save the file in a program like this: http://purebasic.fr/english/viewtopic.p ... &start=105. In this case admin rights are needed to run the app so we have no problem here but is some other cases admin rights are not needed so I must take extra precautions to be sure that the file will be created where the user wants to and not somewhere else.