Writing to Application.StartupPath in Vista

ImDaFrEaK

Well-known member
Joined
Jan 7, 2006
Messages
416
Location
California
Programming Experience
5-10
I have a program that writes a settings file to the Application.StartUp path so that all users access the same settings when the program is running. This works fine in all windows except Vista. What's the deal here? I placed a message box inside the routine to write the file, just before it executes, to give me that exact file path of the writing process and it's exactly where it's supposed to be. It displays "C:\ProgramFile\MyApp\the file." However; the file does not get written here.

Here's the odd thing. If I goto the .exe, right click, and choose Run as Administrator the files are written to the location they should be. So I guess my situation is completely retarded but maybe simple to overcome.

I have two questions, what's the freaking deal with this crap?

How can I tell my application to ALWAYS run as Administrator even on non-admin accounts?

By the way, even if the account I run the program on is an admin account the files are not written. I have to right click and specify run as administrator. Further more. I know the settings file is being written SOMEWHERE!? because the setttings are saved and loaded again. Just not in the correct path and independant for each user. The stupid thing is Vista won't let me find where they are written and like I mentioned, the message box tells me it's being written to the Application.StartPath like I reqeusted. I hate this.


I'm really in a jam here and need any help I can get with this. Thank you so much.
 
Did you try Application.CommonAppDataPath
Documentations says this is:
the path for the application data that is shared among all users.
 
I think with Vista and the User Account Control security system, you should focus on read/write operations in the "Users" folder. This is typically the My Documents, etc. type folders. You can get this using the System.Environment.GetSpecialFolder (I believe that's the method, doing this from memory) and then specify which folder you want. I personally use My Documents/My AppName
 
No, in this case My Documents will not work. However; I have an application in which I do use this functionality. I hope the answer John gave will be correct. What I need is a folder that is universal to each user not specific. I want to write one file and have users share that same file as a settings file. I was using the application.startuppath and building a sub folder to exist along side my program. But as i mentioned above in Vista this dosn't happen. Any folders written to the program files folder is still specific to each user 'UNLESS' you specifically tell the application to run as administrator. Then it functions as normal. The only problem now is you have to approve the application before each run if UAC is on. This is a problem for the app I am writing. I have developed a work around for now and all is wonderful but the folder I am using is just anothe folder that my program creates on it's own. This is not safe b/c not all users will have the location I want available or accessable to all users always. I am going to try John's idea soon. Thank you again Neal, that would have been a good fix and i do use that folder often but in this case I can't.
 
I wouldn't want application runtime and setting files in the Documents folder. Environment.GetFolderPath will only give you the generic base folders, Application class will return these special folders assigned this application.

Here's some example code and their output:
VB.NET:
Dim sb As New System.Text.StringBuilder
sb.AppendLine(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData))
sb.AppendLine(Application.CommonAppDataPath)
sb.AppendLine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData))
sb.AppendLine(Application.UserAppDataPath)
sb.AppendLine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData))
sb.AppendLine(Application.LocalUserAppDataPath)
Clipboard.SetText(sb.ToString)
C:\Documents and settings\All Users\Programdata
C:\Documents and settings\All Users\Programdata\WindowsApplication1\WindowsApplication1\1.0.0.0
C:\Documents and settings\JH\Programdata
C:\Documents and settings\JH\Programdata\WindowsApplication1\WindowsApplication1\1.0.0.0
C:\Documents and settings\JH\Lokale innstillinger\Programdata
C:\Documents and settings\JH\Lokale innstillinger\Programdata\WindowsApplication1\WindowsApplication1\1.0.0.0
 
What's I'm doing in my app to ensure things are written to the installed location is:

VB.NET:
[SIZE=3][/SIZE]Dim dirInfo As IO.DirectoryInfo = IO.Directory.GetParent(My.Application.Info.DirectoryPath)
My.Settings.AppFolder = dirInfo.FullName
'My.Settings.AppFolder is a string created in the My.Settings area[SIZE=3]
[/SIZE][SIZE=3]
[/SIZE]

You could also use:

VB.NET:
My.Settings.AppFolder = System.Reflection.Assembly.GetExecutingAssembly().GetName.CodeBase 'strip off the file:/// if you want.
 
Those get the same as Application.StartupPath basically.
 
Dim dirInfo As IO.DirectoryInfo = IO.Directory.GetParent(My.Application.Info.DirectoryPath)
My.Settings.AppFolder = dirInfo.FullName

My.Settings.AppFolder = System.Reflection.Assembly.GetExecutingAssembly().GetName.CodeBase

Those both refer to the application.startuppath. Pretty much the same thing as John has said. This will work in Windows prior to Vista but Vista does not allow this in the same manner. In order for this to work in Vista you have to allow the program to run as Administrator. This means you have to approve the program everytime it starts if UAC (user account control) is on. <- A stupid and annoying safety feature in Vista but the security it provides is intact.

However; W/O doing this then writing to the Application.startuppath or any of the paths you provided or ANY PATH inside the ProgramFiles Folder will be specific to each user. Not universal. In other words, if the program is not admin Vista redirects the files to be written else where. Where? I don't know. My application still says it's in that path but the path doesn't reflect it in explorer nor does the application find the file with other users. Like I said, this works in Windows prior to Vista but not in Vista.


NOW the work around. JohnH you were correct. The paths you provided are universal to each user even in Vista. This is now where I am saving my settings file. I used Environment.GetPath() function and added my own directory b/c I did notice it returned jsut the path. I did not realize Application.GetPath() would return a more unique directory specific to my application. I will look into this as well but either way. All is well now and problem resolved. If programming with Vista you want to use CommonApplicationData location to prevent security issues or UAC from going off alarming or notifying the user. Thank you both, b/c this has shed light on different options I have and has been very helpfull.

excepted answer
changed read below
 
Last edited:
Not an answer to your specific question but to quote;
By the way, even if the account I run the program on is an admin account the files are not written. I have to right click and specify run as administrator

Do a search for Vista Administrators, you'll see that even as a user set as an adminstrator, you are not actually "the administrator" whilst User Access Control is running!!!

I had software issues on my Vista laptop - turned that damn UAC off and problems disappeared - apart from an annoying popup in the start menu telling me I have a security risk everytime I log on!!
 
Arg81, did you try using the "legal" data paths? Seemed to work for OP. See post 6.
 
this wasn't with software I was developing, it was with software i was installing!
I needed to right click the executables and "run as" the administrator, even though my user has admin rights.

Wasn't all software installs, only a couple of bits....thought I'd share it as from what I found on the web it's a common issue with Vista. - and it was a point made to the OP's quote, and not their problem :)
 
Yeah, i know that same issue. Vista mirrors the directories per user and only the Admin get's the true directory.

And you're correct, to make it work you have to turn UAC off or approve it each and every time it attempts to run.

With my program this isn't feasable but i made it work. I found a way to run as user and still keep the admin standard that i need for my application. Vista sucks in this aspect for developers but to a good point it is more safe.

Basically, what i learned was that only SPECIAL directories are mirrored. You have to create your own directory or install the application to a new path in order to avoid this problem. Hope this helps.
 
Back
Top