Solved

C# - Writing in the directory C:\Users in Windows 7

Posted on 2013-05-13
5
266 Views
Last Modified: 2013-05-15
My apps C# WinForm.
How to write in the C:\Users in Windows 7 with C#?
        public static void Write()
            {
            //IniFile ini=new IniFile(Application.StartupPath+@"\\config.ini");
            IniFile ini=new IniFile("C:\\users\\monitoring_config.ini");

            ini.WriteValue("MAIN","rb_ON",MAIN.rb_ON.ToString());
            ini.WriteValue("MAIN","rb_OFF",MAIN.rb_OFF.ToString());
            }

// This is in IniFile.cs
        [DllImport("kernel32")]
        private static extern long WritePrivateProfileString(string section,
            string key,string val,string filePath);

        public void WriteValue(string Section,string Key,string Value)
            {
            WritePrivateProfileString(Section,Key,Value,this.path);
            }

Open in new window

Thanks in advance,
ocaccy
0
Comment
Question by:ocaccy
  • 2
  • 2
5 Comments
 
LVL 11

Expert Comment

by:b_levitt
ID: 39164459
I'm guessing UAC is not going to let you.  Using "run as administrator" with your program should address the issue.
0
 
LVL 14

Accepted Solution

by:
Vel Eous earned 500 total points
ID: 39164659
As b_levitt has mentioned in their post, UAC will most probably prevent you from writing to this directory due to permission reasons.  You can choose to run the application as an administrator, however, this assumes that you have suitable credentials to authenticate with.  This might not be the case if your application is to be consumed by a standard user.

Rather than reducing your UAC level which is:
Unadvisable
Certain to raise eyebrows if you're developing for a client and request they reduce UAC levels

You should consider writing to one of the many Special Folders made available to you in the Environment.SpecialFolder enumeration.  Some of the more utilised ones include:
Common application data
Program data
User profile

[More folders here]

The folder you write to will be determined by the scope of the file you are attempting to write.  If the file is going to be shared between users to define an applications behaviour then you might want to choose common application data, or user specific local application data.  If the data is user specific and nothing to do with an application the the user profile folder might be ideal.  Choice is yours.

using (System.IO.StreamWriter sw = new System.IO.StreamWriter(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) + @"\config.ini", true))
{
    sw.WriteLine("Your data to write goes here");
}

Open in new window

0
 

Author Comment

by:ocaccy
ID: 39166870
Thank you.

We need to work on Windows Vista and 7.
Let's use this app internally in the company.
All users are administrator.

I used the code above, it worked on my pc W7, but not on others pcs W7 and Vista.
We could manually change the permissions for this folder: C:\Users ?
But we decided that the application should do own it.
What should we do to work on all pcs W7 and Vista the code above?

Thank you in anticipation, ocaccy
0
 
LVL 14

Expert Comment

by:Vel Eous
ID: 39169159
There should not be any issues using the code snippet supplied on Windows 7 or Windows Vista.  I suggest you step through the code in a debugger and see what happens.
0
 

Author Closing Comment

by:ocaccy
ID: 39169940
It worked on my machine without asking for administrator privilege.
Thank you, ocaccy
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C# Comparing String Arrays. 5 39
Why does my excel data start at B1 instead of A1. 9 83
C# bracket error 3 30
transaction in asp.net, sql server 6 33
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question