Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Writing to system32 folder

Posted on 2010-08-28
6
Medium Priority
?
2,635 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 2000 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 98

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
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.

 
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

The Ideal Solution for Multi-Display Applications

Check out ATEN’s VS1912 12-Port DP Video Wall Media Player at InfoComm 2017. Kerri describes how easy it is to design creative video walls in asymmetric layouts and schedule detailed playlists ahead of time with its advanced scheduling feature.

Question has a verified solution.

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

In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

670 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