locked
Error writing text file to system32 directory ? RRS feed

  • Question

  • Hi Evertbody !

    I use C # winform to write the text file to the folder 'C: \ windows \ system32' but it is error, how to fix this error? The error message below:

    unhandled exception has occurred in your application. if you click continue the application will ignore this error and attempt to continue. if you click Quit, the application will close immediately.
    Access to the path 'C:\windows\system32\Help.txt' is denied.


    See the end of this message for details on invoking 
    just-in-time (JIT) debugging instead of this dialog box.

    ************** Exception Text **************
    System.UnauthorizedAccessException: Access to the path 'C:\Windows\system32\Help.txt' is denied.
       at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
       at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
       at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
       at Form.clsFileReadWrite.WriteFile(String FilePath, String Data)
       at Form.clsFileText.MakeFileText()
       at Form.clsFileText.DaysToEnd()
       at Form.Form1.cmdDangnhap_Click(Object sender, EventArgs e)
       at System.Windows.Forms.Control.OnClick(EventArgs e)
       at DevExpress.XtraEditors.BaseButton.OnClick(EventArgs e)
       at DevExpress.XtraEditors.BaseButton.OnMouseUp(MouseEventArgs e)
       at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at DevExpress.Utils.Controls.ControlBase.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


    ************** Loaded Assemblies **************
    mscorlib
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.5420 (Win7SP1.050727-5400)
        CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
    ----------------------------------------
    Form
        Assembly Version: 1.0.0.0
        Win32 Version: 1.0.0.0
        CodeBase: file:///C:/FileReadWrite/Form.exe
    ----------------------------------------
    System.Windows.Forms
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.5420 (Win7SP1.050727-5400)
        CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
    ----------------------------------------
    System
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.5420 (Win7SP1.050727-5400)
        CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
    ----------------------------------------
    System.Drawing
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.5420 (Win7SP1.050727-5400)
        CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
    ----------------------------------------
    DevExpress.Utils.v9.3
        Assembly Version: 9.3.3.0
        Win32 Version: 9.3.3.0
        CodeBase: file:///C:/FileReadWrite/DevExpress.Utils.v9.3.DLL
    ----------------------------------------
    DevExpress.XtraEditors.v9.3
        Assembly Version: 9.3.3.0
        Win32 Version: 9.3.3.0
        CodeBase: file:///C:/FileReadWrite/DevExpress.XtraEditors.v9.3.DLL
    ----------------------------------------
    DevExpress.Data.v9.3
        Assembly Version: 9.3.3.0
        Win32 Version: 9.3.3.0
        CodeBase: file:///C:/FileReadWrite/DevExpress.Data.v9.3.DLL
    ----------------------------------------
    System.Configuration
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.5420 (Win7SP1.050727-5400)
        CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Configuration/2.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
    ----------------------------------------
    System.Xml
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.5420 (Win7SP1.050727-5400)
        CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
    ----------------------------------------
    System.Management
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.5420 (Win7SP1.050727-5400)
        CodeBase: file:///C:/Windows/assembly/GAC_MSIL/System.Management/2.0.0.0__b03f5f7f11d50a3a/System.Management.dll
    ----------------------------------------
    System.Data
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.5459 (Win7SP1GDR.050727-5400)
        CodeBase: file:///C:/Windows/assembly/GAC_32/System.Data/2.0.0.0__b77a5c561934e089/System.Data.dll
    ----------------------------------------
    System.Transactions
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.4927 (NetFXspW7.050727-4900)
        CodeBase: file:///C:/Windows/assembly/GAC_32/System.Transactions/2.0.0.0__b77a5c561934e089/System.Transactions.dll
    ----------------------------------------
    System.EnterpriseServices
        Assembly Version: 2.0.0.0
        Win32 Version: 2.0.50727.5420 (Win7SP1.050727-5400)
        CodeBase: file:///C:/Windows/assembly/GAC_32/System.EnterpriseServices/2.0.0.0__b03f5f7f11d50a3a/System.EnterpriseServices.dll
    ----------------------------------------

    ************** JIT Debugging **************
    To enable just-in-time (JIT) debugging, the .config file for this
    application or computer (machine.config) must have the
    jitDebugging value set in the system.windows.forms section.
    The application must also be compiled with debugging
    enabled.

    For example:

    <configuration>
        <system.windows.forms jitDebugging="true" />
    </configuration>

    When JIT debugging is enabled, any unhandled exception
    will be sent to the JIT debugger registered on the computer
    rather than be handled by this dialog box.

    Wednesday, December 6, 2017 7:09 AM

Answers

  • How many?  As many as Our Friends in Redmond want to have.  

    How do you find them?  Experimentation.  Can you get to it through windows explorer?  If not, you should probably leave well alone.

    Your question is about how to get your WinForm application to write into such directories.  The short answer is you can't.   

    U.A.C. put an end to user processes overwriting critical system files a decade ago.  The only way to get files into "system" directories reliably is with an Installer.  Regular applications must not write to such locations.  Indeed, Microsoft added File System Virtualisation into Windows precisely to get around this problem; attempts by older [or poorly-written] applications to write into these directories are silently redirected into the user's VirtualStore folder instead. 

    To misquote the song: "You fought the UAC and the UAC won".

    Regards,   Phill  W.


    Regards, Phill W.

    • Marked as answer by lamtriendong Thursday, December 14, 2017 1:45 AM
    Monday, December 11, 2017 9:03 AM

All replies

  • Hi lamtriendong,

    >>Access to the path 'C:\windows\system32\Help.txt' is denied.

    This is because the folder "C:\windows\system32" belongs to the system folder, so ordinary users can not operate it, there are two methods to solve it as below:

    1, Run your Visual Studio as administrator.(Not recommended, because we are best not to add unnecessary stuff inside the system folder).

    2, Change the path to another folder just like "D:\somefolder\Help.txt".(Recommended)

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by Bob Ding Thursday, December 7, 2017 6:06 AM
    Wednesday, December 6, 2017 7:30 AM
  • Case: 2 I do already but case: 1 how run my Visual Studio as administrator, how do I do ? do you have specific examples ?

    Wednesday, December 6, 2017 9:55 AM
  • Hi,

    >>but case: 1 how run my Visual Studio as administrator, how do I do ? do you have specific examples ?

    Right click the Visual Studio and select Run as administrator:

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Stanly Fan Wednesday, December 6, 2017 9:59 AM
    • Proposed as answer by Bob Ding Thursday, December 7, 2017 6:06 AM
    Wednesday, December 6, 2017 9:59 AM
  • I agree with you on how to read and write text files, in case I want to copy library files (*.dll, *.sys, *.dat,...) to the system32 directory, how C# code ?
    Thursday, December 7, 2017 1:29 AM
  • Hi lamtriendong,

    >>in case I want to copy library files (*.dll, *.sys, *.dat,...) to the system32 directory, how C# code ?

    Try this:

            private void button1_Click(object sender, EventArgs e)
            {
                string sourceFile = @"D:\New folder\MyDll.dll";
                string destinationFile = @"C:\windows\system32\MyDll.dll";
                FileInfo file = new FileInfo(sourceFile);
                if (file.Exists)
                {
                    // true is overwrite
                    file.CopyTo(destinationFile, true);
                }
            }

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Stanly Fan Thursday, December 7, 2017 1:47 AM
    • Proposed as answer by Bob Ding Thursday, December 7, 2017 6:06 AM
    Thursday, December 7, 2017 1:45 AM
  • Code copy your library files are also error messages when copied to the system32 directory.

    Thursday, December 7, 2017 6:53 AM
  • Hi,

    Have you tried to do it as an administrator?

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Edited by Stanly Fan Thursday, December 7, 2017 6:56 AM
    Thursday, December 7, 2017 6:55 AM
  • C;\Windows\System32 is a restricted Folder that Regular users do not have access to (under User Account Control, UAC). 

    It's been this way for about a decade, so this should be no surprise to you.  

    Your application should not be using this location directly unless you have a very Good Reason to do so. 

    Assuming that you do have a Good Reason to put files into this Folder then your application should not be doing it.  Write an Installer and have the user run that "as Administrator", passing through the UAC Challenge and providing valid credentials as they do so. 


    Regards, Phill W.

    Thursday, December 7, 2017 11:39 AM
  • As you say System32 is a special directory of the windows operating system, so how many special directory (folders) in windows ? And how are the special paths written in C# ?
    Monday, December 11, 2017 1:51 AM
  • Hi,

    I'm afraid we can not know how many, but if you want to operate them, just like I said many times above, run the Visual Studio as administrator.

    Regards,

    Stanly


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, December 11, 2017 1:57 AM
  • How many?  As many as Our Friends in Redmond want to have.  

    How do you find them?  Experimentation.  Can you get to it through windows explorer?  If not, you should probably leave well alone.

    Your question is about how to get your WinForm application to write into such directories.  The short answer is you can't.   

    U.A.C. put an end to user processes overwriting critical system files a decade ago.  The only way to get files into "system" directories reliably is with an Installer.  Regular applications must not write to such locations.  Indeed, Microsoft added File System Virtualisation into Windows precisely to get around this problem; attempts by older [or poorly-written] applications to write into these directories are silently redirected into the user's VirtualStore folder instead. 

    To misquote the song: "You fought the UAC and the UAC won".

    Regards,   Phill  W.


    Regards, Phill W.

    • Marked as answer by lamtriendong Thursday, December 14, 2017 1:45 AM
    Monday, December 11, 2017 9:03 AM
  • Thank you for answering my questions
    Thursday, December 14, 2017 1:46 AM