运行cmd并安装框架

时间:2014-01-19 15:10:32

标签: c# .net winforms cmd

我想在另一台PC上运行我的应用程序。但如果没有安装.net 3.5我就无法运行它。我找到了运行cmd的解决方案并检测其是否为3.5。如果没有,它将开始安装。 但是,如果我启动我的应用程序,那么只需启动cmd,但我的论点是if exist \"%WINDIR%\\Microsoft.Net\\Framework\\v3.5\" goto end start /wait .\\Framework\\dotnetfx.exe /q /norestart\。然后我想立即关闭cmd。有什么建议吗?

public class CmdRun
{
    public CmdRun()
    {
        string cmdMessage ="if exist \"%WINDIR%\\Microsoft.Net\\Framework\\v3.5\" goto end start /wait .\\Framework\\dotnetfx.exe /q /norestart\" :end";
        System.Diagnostics.Process.Start("CMD.exe",cmdMessage);
    }

    public static void CmdRunFunc()
    {
        System.Diagnostics.Process process = new System.Diagnostics.Process();
        System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();
        startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
        startInfo.FileName = "cmd.exe";
        startInfo.Arguments = "if exist \"%WINDIR%\\Microsoft.Net\\Framework\\v3.5\" goto end start /wait .\\Framework\\dotnetfx.exe /q /norestart\" :end";
        process.StartInfo = startInfo;
        process.Start();
    }
}

2 个答案:

答案 0 :(得分:3)

替代解决方案:

转到Solution Explorer > Right Click your project > Properties > Publish > Prerequisites > Choose .NET 3.5 and then choose to download automatically.

然后发布您的申请(Build > Publish - Solution Name

这将强制应用程序下载并安装.NET 3.5。

答案 1 :(得分:2)

这段代码很无用。如果它运行,则意味着已经安装了.NET,因此不再需要检查它。

您应该只创建一个执行检查的批处理文件,然后启动.NET安装程序或启动应用程序。这些方面的东西:

if exist \"%WINDIR%\\Microsoft.Net\\Framework\\v3.5\" goto end 
start /wait .\\Framework\\dotnetfx.exe /q /norestart\" 
:end
start .\\myapp.exe