\'which\' command, in C#


/ Published in: C#
Save to your folder(s)

This code came from the cs-script project. It\'s a C# implementation of the which command.


Copy this code and paste it in your HTML
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using Microsoft.Win32;
  5. using System.Diagnostics;
  6.  
  7. class Script
  8. {
  9. const string usage = "Usage: cscscript which file\nVerifies which copy of the executable file would be executed if invoked from command-prompt.\n"+
  10. "file - name of the executable file\n";
  11.  
  12. static public void Main(string[] args)
  13. {
  14. if (args.Length == 0 ||
  15. (args.Length == 1 && (args[0] == "?" || args[0] == "/?" || args[0] == "-?" || args[0].ToLower() == "help")))
  16. {
  17. Console.WriteLine(usage);
  18. }
  19. else
  20. {
  21. string file = args[0].EndsWith(".exe") ? args[0] : args[0]+".exe";
  22. string filePath = file;
  23.  
  24. if (File.Exists(file))
  25. Console.WriteLine(Path.GetFullPath(filePath));
  26. else
  27. foreach(string dir in Environment.GetEnvironmentVariable("Path").Split(";".ToCharArray()))
  28. {
  29. if (File.Exists(filePath = Path.Combine(dir, file)))
  30. {
  31. Console.WriteLine(filePath);
  32. break;
  33. }
  34. }
  35. }
  36. }
  37. }

URL: http://www.csscript.net/help/Script_library.html

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.