Solved

Writing to system32 folder

Posted on 2010-08-28
6
2,389 Views
Last Modified: 2012-05-10
I am working with an application that is installed as a DLL on the system (stealth program for parents to install where a teenager can't go looking for it, etc.). Therefore I need to store a few files in an accessible location that is out of sight. System32 seems the most logical place for these few files.

The problem is in my C# application running on Windows 7 x64 even with UAC rights passed to the application (run as Admin) a file or folder is never created. I don't get an exception at all either, which is very odd to me. It is as though the file creation is ignored.

How can I write a file to system32 in C#?
System.IO.Directory.CreateDirectory(Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\tcIq");

            FileStream fout = new FileStream(Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\tcIq\test.reg", 

                                            FileMode.OpenOrCreate, FileAccess.Write);

            fout.WriteByte(0);

            fout.Close();

Open in new window

0
Comment
Question by:mwdman
  • 3
  • 2
6 Comments
 
LVL 16

Accepted Solution

by:
Vikram Singh Saini earned 500 total points
ID: 33551229
Hi,

Please check code. I tried with WriteByte(0); but it didn't work. I don't know why.


Regards,
VSS

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ee_Write_to_System32
{
    public partial class Form1 : Form
    {
        string filePath = System.Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\Test\testreport.reg";
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string folderPath = Environment.GetFolderPath(Environment.SpecialFolder.System) + @"\Test";
            Directory.CreateDirectory(folderPath);
            
            FileStream fs = new FileStream(filePath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);

            StreamWriter sw = new StreamWriter(fs, Encoding.UTF8);
            sw.WriteLine(textBox1.Text);

            if (File.Exists(filePath))
            {
                MessageBox.Show("Wow! File created");
            }

            sw.Close();
            fs.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (File.Exists(filePath))
            {
                StreamReader sr= File.OpenText(filePath);

                while (sr.Peek()!=-1)
                {
                    textBox2.Text = sr.ReadLine();
                }

                sr.Close();
            }
        }
    }
}

Open in new window

0
 
LVL 91

Expert Comment

by:John Hurst
ID: 33551254
Are you sure the UAC rights are correct. You need to be a member of the administrators group and have elevated UAC rights to write to the system32 folder (at least to the folders in there that I access and make changes to).
... Thinkpads_User
0
 

Author Comment

by:mwdman
ID: 33551458
The code was helpful to see the file was actually being created and caused me to look further into the problem. It appears the file was being created in C:\Windows\SysWOW64\Test2 even thought the path was C:\Windows\System32

Strange but at least I know the issue now.
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 16

Expert Comment

by:Vikram Singh Saini
ID: 33551465
Hi,

It appears the file was being created in C:\Windows\SysWOW64\Test2 even  thought the path was C:\Windows\System32

But how the file was being created at C:\Windows\SysWOW64\Test2 ? Please share

Thanks and Regards,
VSS
0
 

Author Comment

by:mwdman
ID: 33551480
If I do

MessageBox.Show(Environment.SpecialFolder.System);

It shows C:\Windows\System32

But when I write a file it does to:
C:\windows\SysWOW64

Which is why it appeared to not be working, but in fact was just working differently then expected.

I suspect .NET framework which handles the file IO changes the write location since I'm on a 64-bit OS and that part of the .NET framework is maybe 64-bit (as my app is compiled as x86). I verfied this by writing a C++ program in native code and it wrote to system32... So it is indeed an oddity to the .NET framework maybe something new in VS2010, not sure.
0
 
LVL 16

Expert Comment

by:Vikram Singh Saini
ID: 33551485
Hi,

Thanks for sharing Interesting piece of information.

Regards,
VSS
0

Featured Post

Promote certifications in your email signature

Has your company recently won an award or achieved a certification? They'll no doubt want to show it off. Email signature images used to promote certifications & awards can instantly establish credibility with a recipient and provide you with numerous benefits.

Question has a verified solution.

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

New Windows 7 Installations take days for Windows-Updates to show up and install. This can easily be fixed. I have finally decided to write an article because this seems to get asked several times a day lately. This Article and the Links apply to…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
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.

947 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

18 Experts available now in Live!

Get 1:1 Help Now