Solved

Writing to system32 folder

Posted on 2010-08-28
6
2,449 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 93

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
Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

 
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

Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
defining NULL or 0 10 44
One named event, multiple event handlers 2 24
abstract class C# 1 31
need to convert function to c# 5 24
While working, an annoying popup showing below will come and we cannot cancel or close it form the screen. The error message will come again and again.
An article on effective troubleshooting
This Micro Tutorial will give you a introduction in two parts how to utilize Windows Live Movie Maker to its maximum capability. This will be demonstrated using Windows Live Movie Maker on Windows 7 operating system.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

808 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