Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8814541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T04:09:06+00:00 2026-06-14T04:09:06+00:00

I have a batch file containing the following commands: cd C:\myfolder NuGet Update -self

  • 0

I have a batch file containing the following commands:

cd C:\myfolder
NuGet Update -self
NuGet pack mypackage.nuspec

myfolder contains mypackage.nuspec and NuGet.exe. I try to run this command with C# using the following function:

        private static int ExecuteCommand(string path)
        {
            ProcessStartInfo ProcessInfo;
            Process Process;

            ProcessInfo = new ProcessStartInfo(path);
            ProcessInfo.CreateNoWindow = true;
            ProcessInfo.UseShellExecute = false;
            ProcessInfo.WorkingDirectory = new System.IO.FileInfo(path).DirectoryName;
            ProcessInfo.EnvironmentVariables["EnableNuGetPackageRestore"] = "true";


            // *** Redirect the output ***
            ProcessInfo.RedirectStandardError = true;
            ProcessInfo.RedirectStandardOutput = true;


            Process = Process.Start(ProcessInfo);
            Process.WaitForExit();

            // *** Read the streams ***
            string output = Process.StandardOutput.ReadToEnd();
            string error = Process.StandardError.ReadToEnd();

            int ExitCode = Process.ExitCode;
            Process.Close();
            return ExitCode;

        }

However, my commands are not executed. What is causing this behavior and what is the solution? Those strings will probably be used in the future, I’ll update my question then (just to prevent chriticism :)).

This is the final version of the function:

    private static ShellCommandReturn ExecuteCommand(string path)
    {
        ProcessStartInfo processInfo;
        Process process;

        processInfo = new ProcessStartInfo(path);
        processInfo.CreateNoWindow = true;
        processInfo.UseShellExecute = false;
        processInfo.WorkingDirectory = new System.IO.FileInfo(path).DirectoryName;
        processInfo.EnvironmentVariables["EnableNuGetPackageRestore"] = "true";


        // *** Redirect the output ***
        processInfo.RedirectStandardError = true;
        processInfo.RedirectStandardOutput = true;


        process = Process.Start(processInfo);
        process.WaitForExit();

        // *** Read the streams ***
        string output = process.StandardOutput.ReadToEnd();
        string error = process.StandardError.ReadToEnd();

        int exitCode = process.ExitCode;
        process.Close();
        return new ShellCommandReturn { Error = error, ExitCode = exitCode, Output = output };
    }

ShellCommandReturn is a simple custom class with a few data members where error, output and exit code of a shell command are stored.

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T04:09:07+00:00Added an answer on June 14, 2026 at 4:09 am

    EDIT: After a certain amount of collaboration 🙂

    The problem is that this is executing in the context of a web application, which doesn’t have the same environment variables set.

    Apparently setting:

    startInfo.EnvironmentVariables["EnableNuGetPackageRestore"] = "true"
    

    (using the naming of my final code below) fixes the problem.


    Old answer (still worth reading)

    Look at this code:

    ProcessInfo = new ProcessStartInfo(path);
    ProcessInfo.CreateNoWindow = false;
    ProcessInfo.UseShellExecute = true;
    ProcessInfo.WorkingDirectory = new System.IO.FileInfo(path).DirectoryName;
    
    Process = Process.Start(path);
    

    You’re creating a ProcessStartInfo, but then completely ignoring it. You should be passing it into Process.Start. You should also rename your variables. Conventionally local variables start with lower case in C#. Additionally, it’s a good idea to initialize variables at the point of first use, where possible. Oh, and import namespaces so you don’t fully qualified names such as System.IO.FileInfo in your code. Finally, object initializers are useful for classes like ProcessStartInfo:

    var startInfo = new ProcessStartInfo(path) {
        CreateNoWindow = false,
        UseShellExecute = true,
        WorkingDirectory = new FileInfo(path).DirectoryName;
    };
    var process = Process.Start(startInfo);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a batch file containing a python script using the Output template> %(NAME)s
I have a batch file with the following code: for /f tokens=* %%a in
I have a batch file and I want to include an external file containing
I have following batch file code: @echo off SET INSTALL_PATH=c:\program files\ :ask_again if exist
I have a batch file with some commands that I need to run with
Let's say I have a file containing the following: set_t lorem = 168 set_t
I have a batch file that does the following: @IF EXIST C:\Program Files\MyAppFolder (
I'm trying to accomplish the following ridiculous task: I have a text file containing
I have the following batch file @echo off setlocal EnableDelayedExpansion for /f delims== %%J
I have a batch file with the following 2 lines and change them into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.