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
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?
Re: Problem with Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Quote:
Originally Posted by
PlausiblyDamp
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.
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?
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.
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.
Re: Problem with Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Quote:
Originally Posted by
PlausiblyDamp
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
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
Re: Problem with Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Quote:
Originally Posted by
pmeloy
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
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.