how to write to file in program folder in
Program Files\n\n.ini.
how to write to n.ini in program folder.

Recommended Answers

All 5 Replies

The same way you'd write a file anywhere else. However, the Program Files folder is protected and may not be accessible depending on the user running the application. Impersonating a user with elevated privileges is kind of hairy, so the better approach would be to store your INI files in the ProgramData folder or isolated storage[1].

[1] Assuming, of course, that your goal for this isn't trying to monkey with the INI file of another program not otherwise under your control. ;)

Use one of the following directories to store your data:

Dim userProfileDir As String = Environment.GetEnvironmentVariable("USERPROFILE")

Dim allUsersProfileDir As String = Environment.GetEnvironmentVariable("ALLUSERSPROFILE")

Dim appDataDir As String = Environment.GetEnvironmentVariable("APPDATA")

If you don't need to run your program on XP, you can use:

Dim appDataDir As String = Environment.GetEnvironmentVariable("ProgramData")

If you don't need to run your program on XP, you can use:

Allow me to show you a way to avoid OS dependencies through a standard enumeration: Click Me

is there any way to write without givin admin right to app.

See the post from deceptikon for:

System.Environment.SpecialFolder Enumeration


"...The user's profile folder. Applications should not create files or folders at this level; they should put their data under the locations referred to by ApplicationData. Added in the .NET Framework 4..."

Any of these should work:

'The directory that serves as a common repository for 
'application-specific data for the current roaming user.
'A roaming user works on more than one computer on a network. 
'A roaming user's profile is kept on a server on the network 
'and is loaded onto a system when the user logs on.

Dim applicationData As String = System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Dim appDataEnvVar As String = System.Environment.GetEnvironmentVariable("APPDATA")

'The directory that serves as a common repository for 
'application-specific data that is used by all users.
Dim commonApplicationData As String = System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)
Dim allUsersProfileEnvVar As String = System.Environment.GetEnvironmentVariable("ALLUSERSPROFILE")

'The directory that serves as a common repository for 
'application-specific data that is used by the current, 
'non-roaming user.
Dim localApplicationData As String = System.Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData)

'The My Documents folder.
'This member is equivalent to Personal.
Dim myDocuments As String = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
Dim personal As String = System.Environment.GetFolderPath(Environment.SpecialFolder.Personal)

Console.WriteLine("ApplicationData: " & applicationData)
Console.WriteLine("appDataEnvVar: " & appDataEnvVar)

Console.WriteLine("CommonApplicationData: " & commonApplicationData)
Console.WriteLine("allUsersProfileEnvVar: " & allUsersProfileEnvVar)

Console.WriteLine("LocalApplicationData: " & localApplicationData)
Console.WriteLine("MyDocuments: " & myDocuments)
Console.WriteLine("Personal: " & personal)

Note: Choose the most appropriate one.

For Win 7 (if "C:" is your system drive):

  • ApplicationData should be: "C:\Users\<username>\AppData\Roaming"
  • CommonApplicationData should be "C:\ProgramData"
  • LocalApplicationData should be "C:\Users\<username>\AppData\Local
  • MyDocuments should be "C:\Users\<username>\Documents"
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.