Results 1 to 16 of 16

Thread: Access to the path "C:\Program Files (x86)\....\MyFile.txt" is denied.

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jun 2010
    Posts
    177

    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

  2. #2
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    Re: Access to the path "C:\Program Files (x86)\....\MyFile.txt" is denied.

    Quote Originally Posted by VB-MCU-User View Post
    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.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jun 2010
    Posts
    177

    Re: Access to the path "C:\Program Files (x86)\....\MyFile.txt" is denied.

    Quote Originally Posted by PlausiblyDamp View Post
    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?

  4. #4
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Access to the path "C:\Program Files (x86)\....\MyFile.txt" is denied.

    Quote Originally Posted by VB-MCU-User View Post
    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.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Access to the path "C:\Program Files (x86)\....\MyFile.txt" is denied.

    Quote Originally Posted by VB-MCU-User View Post
    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.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jun 2010
    Posts
    177

    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.

  7. #7
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    Re: Access to the path "C:\Program Files (x86)\....\MyFile.txt" is denied.

    Quote Originally Posted by VB-MCU-User View Post
    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)

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jun 2010
    Posts
    177

    Re: Access to the path "C:\Program Files (x86)\....\MyFile.txt" is denied.

    Quote Originally Posted by OptionBase1 View Post
    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.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Access to the path "C:\Program Files (x86)\....\MyFile.txt" is denied.

    Quote Originally Posted by OptionBase1 View Post
    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.

  10. #10
    PowerPoster yereverluvinuncleber's Avatar
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    2,235

    Re: Access to the path "C:\Program Files (x86)\....\MyFile.txt" is denied.

    Quote Originally Posted by VB-MCU-User View Post
    "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.

  11. #11

    Thread Starter
    Addicted Member
    Join Date
    Jun 2010
    Posts
    177

    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.

  12. #12
    Fanatic Member
    Join Date
    Jun 2019
    Posts
    557

    Re: Access to the path "C:\Program Files (x86)\....\MyFile.txt" is denied.

    Quote Originally Posted by VB-MCU-User View Post
    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!

  13. #13
    PowerPoster jdc2000's Avatar
    Join Date
    Oct 2001
    Location
    Idaho Falls, Idaho USA
    Posts
    2,393

    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.

  14. #14
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    845

    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)

  15. #15
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: Access to the path "C:\Program Files (x86)\....\MyFile.txt" is denied.

    Quote Originally Posted by VB-MCU-User View Post
    why are they doing that if it is wrong to do it?
    Because they think that what they want is more important.

  16. #16

    Thread Starter
    Addicted Member
    Join Date
    Jun 2010
    Posts
    177

    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
  •  



Click Here to Expand Forum to Full Width