Solved

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

Posted on 2013-05-13
5
270 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
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…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…

726 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