Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

636 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