-
Apr 20th, 2021, 07:14 PM
#1
Thread Starter
Addicted Member
Access to the path "C:\Program Files (x86)\....\MyFile.txt" is denied.
Hi. The first time I run this program works fine. Now, if I close the program and open it again I receive this error that crashes the program "Access to the path "C:\Program Files (x86)\....\MyFile.txt" is denied.". It doesn't happen in the bin directory when I'm coding. It only happens when I install the program in the "C:\Program Files (x86)" directory with an installer. Is there a way to get around this issue? Thanks a lot.
Code:
Try
Dim UpdateLogFilePosition As Boolean = False
'INCLUDE THE DATE IN THE FILE'S NAME
Dim LogFilePositionDate As String
LogFilePositionDate = DateTime.Now.ToString("d").ToString()
LogFilePositionDate = Replace(LogFilePositionDate, "/", "-") 'ELIMINATES THE "/" FROM THE FILE NAME.
UpdateLogFilePosition = True
If UpdateLogFilePosition = True Then
GetFENNotationForPython()
Dim LogPositionFile As System.IO.StreamWriter
LogPositionFile = My.Computer.FileSystem.OpenTextFileWriter(Application.StartupPath & "\BoardPositionsLog\PositionsLog_" & LogFilePositionDate & ".txt", True)
LogPositionFile.WriteLine(FENPosition)
LogPositionFile.Close()
End If
Catch ex As Exception
For IVar = 0 To 63
LogFileLastBoardPosition(IVar) = BoardPosition(IVar)
Next
MessageBox.Show(ex.Message) 'HERE THE PROGRAM CRASHES.
Exit Sub
End Try
-
Apr 20th, 2021, 07:24 PM
#2
Re: Access to the path "C:\Program Files (x86)\....\MyFile.txt" is denied.
Originally Posted by VB-MCU-User
Hi. The first time I run this program works fine. Now, if I close the program and open it again I receive this error that crashes the program "Access to the path "C:\Program Files (x86)\....\MyFile.txt" is denied.". It doesn't happen in the bin directory when I'm coding. It only happens when I install the program in the "C:\Program Files (x86)" directory with an installer. Is there a way to get around this issue? Thanks a lot.
Code:
Try
Dim UpdateLogFilePosition As Boolean = False
'INCLUDE THE DATE IN THE FILE'S NAME
Dim LogFilePositionDate As String
LogFilePositionDate = DateTime.Now.ToString("d").ToString()
LogFilePositionDate = Replace(LogFilePositionDate, "/", "-") 'ELIMINATES THE "/" FROM THE FILE NAME.
UpdateLogFilePosition = True
If UpdateLogFilePosition = True Then
GetFENNotationForPython()
Dim LogPositionFile As System.IO.StreamWriter
LogPositionFile = My.Computer.FileSystem.OpenTextFileWriter(Application.StartupPath & "\BoardPositionsLog\PositionsLog_" & LogFilePositionDate & ".txt", True)
LogPositionFile.WriteLine(FENPosition)
LogPositionFile.Close()
End If
Catch ex As Exception
For IVar = 0 To 63
LogFileLastBoardPosition(IVar) = BoardPosition(IVar)
Next
MessageBox.Show(ex.Message) 'HERE THE PROGRAM CRASHES.
Exit Sub
End Try
By default you need admin permissions to write to anything under the Program files folder, you really shouldn't be writing anything there. Windows provides several locations you should be using instead, .Net provides the https://docs.microsoft.com/en-us/dot...h?view=net-5.0 function to obtain one of the built in locations to use. https://docs.microsoft.com/en-us/dot...r?view=net-5.0 documents all the supported options, you are probably wanting to choose something like LocalApplicationData or CommonApplicationData depending on your exact requirements but it is worth looking at the list and seeing which would make most sense.
-
Apr 20th, 2021, 07:54 PM
#3
Thread Starter
Addicted Member
Re: Access to the path "C:\Program Files (x86)\....\MyFile.txt" is denied.
Originally Posted by PlausiblyDamp
By default you need admin permissions to write to anything under the Program files folder, you really shouldn't be writing anything there. Windows provides several locations you should be using instead, .Net provides the https://docs.microsoft.com/en-us/dot...h?view=net-5.0 function to obtain one of the built in locations to use. https://docs.microsoft.com/en-us/dot...r?view=net-5.0 documents all the supported options, you are probably wanting to choose something like LocalApplicationData or CommonApplicationData depending on your exact requirements but it is worth looking at the list and seeing which would make most sense.
I think you are 100% right . I have seen some programs that instead of creating the installation under C:\Program Files (x86)\, they install the program under a custom directory like C:\MyCompanyName\. I know some people don't like this solution, but I have seen it many times and it works. What do you think about it?
-
Apr 20th, 2021, 07:58 PM
#4
Re: Access to the path "C:\Program Files (x86)\....\MyFile.txt" is denied.
Originally Posted by VB-MCU-User
I think you are 100% right . I have seen some programs that instead of creating the installation under C:\Program Files (x86)\, they install the program under a custom directory like C:\MyCompanyName\. I know some people don't like this solution, but I have seen it many times and it works. What do you think about it?
That's a terrible solution. The Program Files folder is there and configured as it is for a reason. It is where Program Files should go. It is not where Application Data should go. There is no reason that your log files and your .exe file need to exist in the same folder.
-
Apr 20th, 2021, 07:58 PM
#5
Re: Access to the path "C:\Program Files (x86)\....\MyFile.txt" is denied.
Originally Posted by VB-MCU-User
I think you are 100% right . I have seen some programs that instead of creating the installation under C:\Program Files (x86)\, they install the program under a custom directory like C:\MyCompanyName\. I know some people don't like this solution, but I have seen it many times and it works. What do you think about it?
It's a bad solution. The proper solution is to install applications where applications are supposed to be installed and then store data files where data files are supposed to be installed. The former is under the Program Files folder and the latter is not. You've already been provided the proper solution. Windows has a number of dedicated folders for dedicated purposes and a good developer makes use of them. Their paths can be determined using Environment.GetFolderPath and/or My.Computer.FileSystem.SpecialDirectories.
-
Apr 20th, 2021, 08:01 PM
#6
Thread Starter
Addicted Member
Re: Access to the path "C:\Program Files (x86)\....\MyFile.txt" is denied.
Okay, I'll study the two links provided by OptionBase1. That's beyond my skills level, but I'll give it a try.
-
Apr 20th, 2021, 08:13 PM
#7
Re: Access to the path "C:\Program Files (x86)\....\MyFile.txt" is denied.
Originally Posted by VB-MCU-User
Okay, I'll study the two links provided by OptionBase1. That's beyond my skills level, but I'll give it a try.
For the record, it wasn't me that gave the links, it was PlausiblyDamp.
And you already have code that writes to a path. All you need to do is change that one line of code so that it writes to something like the Common Application Data folder rather than what you have now.
Since you are currently using string concatenation, so will I in my below "fix", but you should look in to using Path.Combine.
https://docs.microsoft.com/en-us/dot...e?view=net-5.0
Try this:
Code:
My.Computer.FileSystem.OpenTextFileWriter(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) & "\BoardPositionsLog\PositionsLog_" & LogFilePositionDate & ".txt", True)
-
Apr 20th, 2021, 08:17 PM
#8
Thread Starter
Addicted Member
Re: Access to the path "C:\Program Files (x86)\....\MyFile.txt" is denied.
Originally Posted by OptionBase1
For the record, it wasn't me that gave the links, it was PlausiblyDamp.
And you already have code that writes to a path. All you need to do is change that one line of code so that it writes to something like the Common Application Data folder rather than what you have now.
Since you are currently using string concatenation, so will I in my below "fix", but you should look in to using Path.Combine.
https://docs.microsoft.com/en-us/dot...e?view=net-5.0
Try this:
Code:
My.Computer.FileSystem.OpenTextFileWriter(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) & "\BoardPositionsLog\PositionsLog_" & LogFilePositionDate & ".txt", True)
I'm sorry PlausiblyDamp. Thank you OptionBase1. I'll give it a try.
-
Apr 20th, 2021, 08:18 PM
#9
Re: Access to the path "C:\Program Files (x86)\....\MyFile.txt" is denied.
Originally Posted by OptionBase1
you should look in to using Path.Combine.
Indeed. A bit of string interpolation to help clean things up too.
Code:
My.Computer.FileSystem.OpenTextFileWriter(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
$"BoardPositionsLog\PositionsLog_{LogFilePositionDate}.txt"),
True)
It would also be appropriate to put the application name into the project settings and then get it back from there, rather than hard-coding it.
-
Apr 21st, 2021, 04:53 AM
#10
Re: Access to the path "C:\Program Files (x86)\....\MyFile.txt" is denied.
Originally Posted by VB-MCU-User
"Access to the path "C:\Program Files (x86)\....\MyFile.txt" is denied."
For your information, the change to store any or all of your program's data in appData and away from the "program files" folder, occurred when Vista was implemented. You are a few years out of date with your original storage approach.
-
Apr 21st, 2021, 11:49 AM
#11
Thread Starter
Addicted Member
Re: Access to the path "C:\Program Files (x86)\....\MyFile.txt" is denied.
Thank you all. I'm working on these changes now. However, I have a question related to this. When I opened a folder showing my C drive, I see like 9 or 10 programs that were installed right in the C directory. Some of these software packages costed us a lot of money. So, why are they doing that if it is wrong to do it? Thanks for your help.
-
Apr 21st, 2021, 11:57 AM
#12
Re: Access to the path "C:\Program Files (x86)\....\MyFile.txt" is denied.
Originally Posted by VB-MCU-User
Thank you all. I'm working on these changes now. However, I have a question related to this. When I opened a folder showing my C drive, I see like 9 or 10 programs that were installed right in the C directory. Some of these software packages costed us a lot of money. So, why are they doing that if it is wrong to do it? Thanks for your help.
Because they (the devs of these expensive apps) didn't read VBForums? :-)
I've seen some apps that have million users to put their executable in the appdata folder. Again they didn't read VBForums...
What a shame!
-
Apr 21st, 2021, 12:30 PM
#13
Re: Access to the path "C:\Program Files (x86)\....\MyFile.txt" is denied.
So, why are they doing that?
Probably because they don't want to have to update their code and/or install scripts every time Microsoft decides to change the rules on where files are supposed to go. They don't get any extra profit from doing that. While file locations have been consistent through the last few versions of Windows, there is no guarantee that will always be the case.
-
Apr 21st, 2021, 03:41 PM
#14
Re: Access to the path "C:\Program Files (x86)\....\MyFile.txt" is denied.
As windows had a Maximum Path Length Limitation of 260 characters (before some new update). It can be also a way to reduce the risk of reaching the limit.
The best friend of any programmer is a search engine
"Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
“They did not know it was impossible so they did it” (Mark Twain)
-
Apr 21st, 2021, 07:06 PM
#15
Re: Access to the path "C:\Program Files (x86)\....\MyFile.txt" is denied.
Originally Posted by VB-MCU-User
why are they doing that if it is wrong to do it?
Because they think that what they want is more important.
-
Apr 23rd, 2021, 11:34 PM
#16
Thread Starter
Addicted Member
Re: Access to the path "C:\Program Files (x86)\....\MyFile.txt" is denied.
Thanks for the explanation.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|