Results 1 to 10 of 10

Thread: Problem with Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

  1. #1

    Thread Starter
    New Member
    Join Date
    Sep 2017
    Posts
    5

    Problem with Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

    Hello everybody:

    I am developing a VB.Net project. I need to read a file that is stored in c:\users\francescj\AppData\Roaming\XXX\
    I use this code to access to that folder.

    System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "XXX\")

    I am trying the program in a Windows 7 64bits and using the code I get the following path that is not accessible:

    c:\windows\system32\config\systemprofile\AppData\Roaming\XXX

    The path that I want to access is :
    c:\users\francescj\AppData\Roaming\XXX\

    Anyone knows how can i get it? Which would be the correct code?

    Thanks in advance.

    francescj

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

    Re: Problem with Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

    How are you running the application? Is it being run as a normal user or are you doing something clever?

  3. #3

    Thread Starter
    New Member
    Join Date
    Sep 2017
    Posts
    5

    Re: Problem with Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

    Quote Originally Posted by PlausiblyDamp View Post
    How are you running the application? Is it being run as a normal user or are you doing something clever?
    I run it as a normal user.

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

    Re: Problem with Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

    That is very strange, is this a web application or similar or a typical console or windows forms project?

  5. #5
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Problem with Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

    I think you are being accidentally dishonest.

    You showed us a line of code that should produce the expected path. But you didn't show us the variable you assign it to, and every line of code that uses the variable. You're trying to help us by hiding things you don't think we /need/ to see.

    Since you're not getting the expected results, it's easier to believe "there is a line of code that changes the value you have not showed us" than it is to believe "a 14-year-old API has a new bug". So we actually need to see all of those lines, because it's more likely you made a mistake than the Environment.GetSpecialFolder() method has a bug.
    Last edited by Sitten Spynne; Sep 13th, 2017 at 11:48 AM.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

  6. #6
    Addicted Member
    Join Date
    Dec 2012
    Posts
    136

    Re: Problem with Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

    Path.Combine() already adds all the slashes you'll need, get rid of it from your string.

    Sitten is also correct, Path.Combine() is giving you back exactly what you asked for. The problem is either going to be in what you asked for. In this case what you asked for has nothing to do with the path you are getting. Either you're confusing your variables and getting the one that contains a different path or there's something screwed up about your windows install.

  7. #7

    Thread Starter
    New Member
    Join Date
    Sep 2017
    Posts
    5

    Re: Problem with Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

    Quote Originally Posted by PlausiblyDamp View Post
    That is very strange, is this a web application or similar or a typical console or windows forms project?
    It is a windows forms project.

    francescj

  8. #8

    Thread Starter
    New Member
    Join Date
    Sep 2017
    Posts
    5

    Re: Problem with Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

    Code:
     
    
    Private Function GetPath() As String
            Return System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Params\")
    End Function
    
    Public Function OpenXML(ByVal strFile As String) As Boolean
    
            Dim fileName As String
            Try
    
                fileName = System.IO.Path.Combine(GetPath, strFile )
    
                xmlDoc.Load(fileName)
                return True
    
            Catch ex As Exception
                return False
            End Try
    
    End Function
    
    
            'this code is written in other function 
            If cls_xml.OpenXML("parameters.xml") = False Then
                MsgBox("Impossible to read xml file.", vbCritical + vbOKOnly, "ERROR")
                Exit Sub
            End If
    This is the code I use inside the program.
    Thanks in advance.

    francescj

  9. #9

    Thread Starter
    New Member
    Join Date
    Sep 2017
    Posts
    5

    Re: Problem with Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

    Quote Originally Posted by pmeloy View Post
    Path.Combine() already adds all the slashes you'll need, get rid of it from your string.

    Sitten is also correct, Path.Combine() is giving you back exactly what you asked for. The problem is either going to be in what you asked for. In this case what you asked for has nothing to do with the path you are getting. Either you're confusing your variables and getting the one that contains a different path or there's something screwed up about your windows install.
    Code:
     Public Function GetPath() As String
            MsgBox(System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Params"))
            Return System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Params")
        End Function
    Only using this code I get what I mentined.

    Thank you very much.

    francescj

  10. #10
    You don't want to know.
    Join Date
    Aug 2010
    Posts
    4,578

    Re: Problem with Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

    OK, that made me look a little harder (as in I did a random Google search to try and figure out if there was a sane reason this might happen.) There IS a sane reason for this:

    https://superuser.com/questions/7047...ow-to-solve-it

    You can redirect the user profile to different directories, it seems like sometimes Windows screws up and does this on its own. Technically most users wouldn't notice. So double-check in the Registry to see if the profile for the user has been redirected. If that's true, then technically your code is right. The point of GetFolderPath() is "tell me where Windows thinks this directory exists", and that registry key defines the answer.

    (I've actually done this on my machine. I have a small solid-state drive I bought years ago, so to save room I moved several of my user directories onto a very large "normal" hard drive.)

    Likewise, if that's true, that's where you should be storing the file.
    This answer is wrong. You should be using TableAdapter and Dictionaries instead.

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