Solved

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

Posted on 2013-05-13
5
263 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
Comment Utility
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:
Tchuki earned 500 total points
Comment Utility
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
Comment Utility
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:Tchuki
Comment Utility
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
Comment Utility
It worked on my machine without asking for administrator privilege.
Thank you, ocaccy
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now