Can C# programs access the Sysprep program?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: Can C# programs access the Sysprep program?

  1. #1
    Join Date
    Feb 2014
    Posts
    3

    Can C# programs access the Sysprep program?

    I have an extremely simple program -- so simple, in fact, that it does only one thing: attempts to run Sysprep.exe (C:\Windows\System32\Sysprep\sysprep.exe):

    Process.Start("C:\Windows\System32\Sysprep\sysprep.exe");

    When I run the above snippet, an error occurs indicating that the file called by Process.Start() cannot be found. This happens if I am running the C# program as a standard user or as an administrator user -- the result is the same either way.

    However, I can copy and paste that exact path into Windows Explorer, and Sysprep runs just fine. I can also copy and paste the path into a Command Prompt window, and it runs fine there, as well.

    If I place a batch (*.bat) file or an executable (*.exe) in the "C:\Windows\System32\Sysprep" folder, my C# program cannot see those. However, if I place them in a folder just one level higher ("C:\Windows\System32"), my C# program can see them.

    I thought this might be a permissions issue, but even when I take ownership of the Sysprep folder and give my user account explicit full control of the folder, nothing changes. My C# program still cannot touch anything in that folder.

    I am targeting .NET 4.5.

    Any ideas on what could be preventing my C# program from accessing Sysprep.exe?

  2. #2
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,958

    Re: Can C# programs access the Sysprep program?

    Sure. You are not TrustedInstaller (which owns the rights). Sounds like Windows x64
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Feb 2014
    Posts
    3

    Re: Can C# programs access the Sysprep program?

    It is Windows 7 64-bit.

    As I said, though, even when I take ownership of the folder, the same problem occurs. And I can access it even as a standard/limited user, so long as I don't try accessing it using a C# program.

    Is there anything inherent about C#/managed programs that would disallow them to access the Sysprep folder?

  4. #4
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,958

    Re: Can C# programs access the Sysprep program?

    No. Windows elevates to TrustedInstaller privileges. C# doesn't

    Look at Powershell. I'd bet it's a 2-5 line script.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  5. #5
    Join Date
    Feb 2014
    Posts
    3

    Re: Can C# programs access the Sysprep program?

    Yeah, I actually have a PowerShell script that will do this already.

    Calling Sysprep is not all I want to do with this program, but that's one of the bare-minimum requirements for the program. I figured if I can't get it to do at least that, there is no reason to move to step two.

    If I used a C# program to call a PowerShell program that called Sysprep, would I run into the same permissions issue?

  6. #6
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,958

    Re: Can C# programs access the Sysprep program?

    I haven't tried. It all depends on the OS, I guess. 64bit is tough. Server is tougher
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  7. #7
    Join Date
    May 2012
    Posts
    25

    Re: Can C# programs access the Sysprep program?

    Hi JasonF,

    Try using this code. I left the args line in, if you want to pass an unattend.xml file to it.

    I also, used manifest files with UAC elevated, if you are still having rights issues.

    <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />

    If you need more help on the manifest file, please let me know, I can help you more.

    Take Care,

    -Mike

    Code:
    ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.FileName = @"C:\windows\sysnative\sysprep\sysprep.exe";
                startInfo.Verb = "runas";
                //startInfo.Arguments = f;
                Process.Start(startInfo);

  8. #8
    Join Date
    Jan 2006
    Location
    Chicago, IL
    Posts
    14,958

    Re: Can C# programs access the Sysprep program?

    Wrong folder? "C:\Windows\System32\Sysprep\sysprep.exe" is the correct one
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  9. #9
    Join Date
    May 2012
    Posts
    25

    Re: Can C# programs access the Sysprep program?

    Native is the correct folder, check MSDN, for System32, on 64-bit system.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center