KillUtil.cs :  » Build-Systems » CruiseControl.NET » ThoughtWorks » CruiseControl » Core » Util » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Build Systems » CruiseControl.NET 
CruiseControl.NET » ThoughtWorks » CruiseControl » Core » Util » KillUtil.cs
using System;
using System.Diagnostics;
using System.IO;

namespace ThoughtWorks.CruiseControl.Core.Util{
    class KillUtil
    {
        /// <summary>
        /// Default installation directory for the "Windows 2000 Service Pack 4 Support Tools" package.
        /// </summary>
        public const string WIN2K_SUPPORT_TOOLS_DIR = @"C:\\Program Files\\Support Tools";

    // TODO: Come back here some day when MS fixed the Process.Kill() bug (see CCNET-815)
    // process.Kill();
        public static void KillPid(int pid)
        {
            Process process = new Process();
            string platform = string.Empty;
           
            switch (Environment.OSVersion.Platform)
            {
                case PlatformID.Win32NT:
                    if ((Environment.OSVersion.Version.Major == 5) && (Environment.OSVersion.Version.Minor == 0))
                    {
                        // Windows 2000 doesn't have taskkill.exe, so use kill.exe from the 
                        // "Windows 2000 Service Pack 4 Support Tools" package from Microsoft's download center
                        // (http://www.microsoft.com/Downloads/details.aspx?FamilyID=f08d28f3-b835-4847-b810-bb6539362473&displaylang=en)
                        // instead.  It may not exist, but if it doesn't, at least if can be obtained.
                        process.StartInfo.FileName = string.Format("{0}\\kill.exe", WIN2K_SUPPORT_TOOLS_DIR);
                        process.StartInfo.Arguments = string.Format("-f {0}", pid);
                        platform = "Windows";
                        break;
                    }
                    else
                    {
                        process.StartInfo.FileName = string.Format("{0}\\taskkill.exe",
                                Environment.GetFolderPath(Environment.SpecialFolder.System));
                        process.StartInfo.Arguments = string.Format("/pid {0} /t /f", pid);
                        platform = "Windows";
                        break;
                    }

               case PlatformID.Unix:
                                      
                    // need to execute uname -s to find out if it is a MAC or not
                    Process unameprocess = new Process();
                    process.StartInfo.FileName = "uname";
                    process.StartInfo.Arguments = "-s";
                    process.StartInfo.CreateNoWindow = true;
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.UseShellExecute = false;

                    process.Start();
                    process.WaitForExit();

                    StreamReader soReader = process.StandardOutput;

                    string output = soReader.ReadToEnd();

                    int nRet = process.ExitCode;

                    process.Close();                                        

                    if ((nRet == 0) && (output.Contains("Darwin")))
                    {
                        process.StartInfo.FileName = "/bin/kill";
                        process.StartInfo.Arguments = string.Format("-9 {0}", pid);
                        platform = "Mac";

                    }
                    else
                    {
                        process.StartInfo.FileName = "/usr/bin/pkill";
                        process.StartInfo.Arguments = string.Format("-9 -g {0}", pid);
                        platform = "Unix";
                    }

                    break;  
                
                default:
                    throw new Exception("Unknown Operating System.");
            }

            if (!File.Exists(process.StartInfo.FileName))
            {
                throw new Exception(string.Format("Kill command {0} not found on {1} OS. PID:{2}",
                                                  process.StartInfo.FileName, platform, Convert.ToString(pid)));
            }

            process.Start();
            process.WaitForExit();
            process.Close();
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.