Solved

Writing to system32 folder

Posted on 2010-08-28
6
2,479 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
[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
  • 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 94

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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Simple, centralized multimedia control

Watch and learn to see how ATEN provided an easy and effective way for three jointly-owned pubs to control the 60 televisions located across their three venues utilizing the ATEN Control System, Modular Matrix Switch and HDBaseT extenders.

Question has a verified solution.

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

By default the complete memory dump option is disabled in windows . If we want to enable the complete memory dump for a diagnostic purpose, we have a solution for it. here we are using the registry method to enable this.
This article summaries thoughts and ideas from two years of sustained use. It provides good reasoning to make the jump to Windows 10.
This Micro Tutorial will teach you the basics of configuring your computer to improve its speed. It will also teach you how to disable programs that are running in the background simultaneously. This will be demonstrated using Windows 7 operating…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

749 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