Solved

Writing to system32 folder

Posted on 2010-08-28
6
2,362 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
Comment Utility
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 90

Expert Comment

by:John Hurst
Comment Utility
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
Comment Utility
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
How does your email signature look on mobiles?

Do your employees use mobile devices to reply to emails? With mobile becoming increasingly important to the business world, it is in your best interest to make sure that your email signature looks great across all types of devices.

 
LVL 16

Expert Comment

by:Vikram Singh Saini
Comment Utility
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
Comment Utility
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
Comment Utility
Hi,

Thanks for sharing Interesting piece of information.

Regards,
VSS
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This is an article about Leadership and accepting and adapting to new challenges. It focuses mostly on upgrading to Windows 10.
A Bare Metal Image backup allows for the restore of an entire system to a similar or dissimilar hardware. They are highly useful for migrations and disaster recovery. Bare Metal Image backups support Full and Incremental backups. Differential backup…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

763 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

12 Experts available now in Live!

Get 1:1 Help Now