?
Solved

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

Posted on 2013-05-13
5
Medium Priority
?
271 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 2000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
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.…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses

800 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