loaded app at startup cannot create file
- doctorized
- Addict

- Posts: 882
- Joined: Fri Mar 27, 2009 9:41 am
- Location: Athens, Greece
loaded app at startup cannot create file
I make a simple app that is loaded at windows startup via registry value and works saving some data in a log file.
When I double-click the app it runs fine. When the app is loaded from the windows at startup fails to create a file
and write in it. I understand that my app has not the right to do it but what can I do?
When I double-click the app it runs fine. When the app is loaded from the windows at startup fails to create a file
and write in it. I understand that my app has not the right to do it but what can I do?
-
Little John
- Addict

- Posts: 4869
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Re: loaded app at startup cannot create file
Choose a location for the file, where your program has the right to write. Note that the proper location might depend on the user account, on which the program is started. For getting a proper path, you can use e.g. the environment variable %appdata%, or a Win API function such as SHGetSpecialFolderLocation_().
The easiest way is to choose a path, where the program always will be allowed to write, e.g. the temp directory:
The easiest way is to choose a path, where the program always will be allowed to write, e.g. the temp directory:
Code: Select all
; Example (PB 5.11)
filePath$ = GetTemporaryDirectory() + "log.txt" ; chose a path here, where your program has the right to write
ofn = OpenFile(#PB_Any, filePath$, #PB_File_Append)
WriteStringN(ofn, "Test")
CloseFile(ofn)- doctorized
- Addict

- Posts: 882
- Joined: Fri Mar 27, 2009 9:41 am
- Location: Athens, Greece
Re: loaded app at startup cannot create file
The user is administrator, the user that was created at windows installation. I tried many different folders with no success, like the folder where the app is in and a folder in %appdata%/local. No luck.Little John wrote:Note that the proper location might depend on the user account, on which the program is started.
Re: loaded app at startup cannot create file
maybe you should use GetLastError_() to get the failure info first?
poor English...
PureBasic & Delphi & VBA
PureBasic & Delphi & VBA
- doctorized
- Addict

- Posts: 882
- Joined: Fri Mar 27, 2009 9:41 am
- Location: Athens, Greece
Re: loaded app at startup cannot create file
Error code: 183 = "Cannot create a file when that file already exists."leonhardt wrote:maybe you should use GetLastError_() to get the failure info first?
And guess what.... the file does not exist!!
Code: Select all
i.l = CreateFile(#PB_Any, GetCurrentDirectory() + "log.txt")
WriteString(i,"test text")
CloseFile(i)
Buffer.s = Space(4096)
dw.l = GetLastError_()
Chars.l = FormatMessage_(#FORMAT_MESSAGE_FROM_SYSTEM, 0, dw, 0, Buffer, Len(Buffer), 0)
MessageRequester(Str(dw), Left(Buffer, Chars-2))Re: loaded app at startup cannot create file
What is the current directory set to?
-
Little John
- Addict

- Posts: 4869
- Joined: Thu Jun 07, 2007 3:25 pm
- Location: Berlin, Germany
Re: loaded app at startup cannot create file
In addition to this question:Demivec wrote:What is the current directory set to?
doctorized, what happens when in your above code you replace GetCurrentDirectory() with GetTemporaryDirectory()?
And BTW, which Windows version do you use?
Re: loaded app at startup cannot create file
Your autostart will not set currentdirectory, better use:
The explorer set the currentdirectory, but the shell doesn't!
Code: Select all
GetPathPart(ProgramFilename())PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.

Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.

- doctorized
- Addict

- Posts: 882
- Joined: Fri Mar 27, 2009 9:41 am
- Location: Athens, Greece
Re: loaded app at startup cannot create file
That's the solution to my problem! My directory is C:\Users\Doctorized\AppData\Local\MyApp.ts-soft wrote:Your autostart will not set currentdirectory, better use:The explorer set the currentdirectory, but the shell doesn't!Code: Select all
GetPathPart(ProgramFilename())
I changed
Code: Select all
MessageRequester(Str(dw), Left(Buffer, Chars-2))Code: Select all
MessageRequester(GetCurrentDirectory(), Str(dw) + " : " + Left(Buffer, Chars-2))error code was 0 (successful). By changing it to GetPathPart(ProgramFilename()) the path was correct and the file
was created. Thanx a lot for the help!!
- doctorized
- Addict

- Posts: 882
- Joined: Fri Mar 27, 2009 9:41 am
- Location: Athens, Greece
Re: loaded app at startup cannot create file
According to PB help for CreateFile(): If the file already exists, it will be overwritten by the new empty file.
but if the file exists, error 183 pops up. Any suggestions?
but if the file exists, error 183 pops up. Any suggestions?
Re: loaded app at startup cannot create file
Before you said:doctorized wrote: By changing it to GetPathPart(ProgramFilename()) the path was correct and the file was created.
How do you explain that ?tried many different folders with no success, like the folder where the app is in
"Have you tried turning it off and on again ?"
- doctorized
- Addict

- Posts: 882
- Joined: Fri Mar 27, 2009 9:41 am
- Location: Athens, Greece
Re: loaded app at startup cannot create file
Very easy!! I was having the app in a different folder every time and GetCurrentDirectory() was used.luis wrote:Before you said:doctorized wrote: By changing it to GetPathPart(ProgramFilename()) the path was correct and the file was created.
How do you explain that ?tried many different folders with no success, like the folder where the app is in
I was thinking that GetCurrentDirectory() was giving the correct path in every case. I must confess that in some
cases, I didn't use GetCurrentDirectory() but a constant string and then I had the problem too. Weird, I know, but true!
Re: loaded app at startup cannot create file
You should only use GetCurrentDirectory(), if you have set it by yourself.
The user of your app can start it in many ways, and only some ways set
the currentdirectory to your applicationfolder
The user of your app can start it in many ways, and only some ways set
the currentdirectory to your applicationfolder
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.

Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.

- doctorized
- Addict

- Posts: 882
- Joined: Fri Mar 27, 2009 9:41 am
- Location: Athens, Greece
Re: loaded app at startup cannot create file
You are right my friend but I didn't know that. Now I know....ts-soft wrote:You should only use GetCurrentDirectory(), if you have set it by yourself.
The user of your app can start it in many ways, and only some ways set
the currentdirectory to your applicationfolder

