get the startup directory

Get the startup directory programmatically. The startup directory is where the .exe was found.

Download getstartupdirectory.zip

Synopsis:

getstartupdirectory.cs
getstartupdirectory_test.cs


getstartupdirectory.cs

Synopsis
using System;
using System.Runtime.InteropServices;

  public class getstartupdirectory
  {
    internal delegate string getcmdlineDelegate();
    internal static getcmdlineDelegate func = new getcmdlineDelegate(getcmdline);
    
    public static string StartupDirectory
    {
      get 
      {
        string commandLine = func().Trim();
        if (commandLine[0] == '\"')
        {
          commandLine = commandLine.Substring(1, commandLine.IndexOf("\"", 1));
        }
        else
        {
          int ispace = commandLine.IndexOf(" ");
          if (ispace > 0)
            commandLine = commandLine.Substring(0, ispace);
        }
        return commandLine.Substring(0, commandLine.LastIndexOf('\\'));
      }
    }

    [ DllImport( "Kernel32.dll", CharSet=CharSet.Auto )] 
    private static extern IntPtr GetCommandLine();   
    internal static string getcmdline()
    {
      return Marshal.PtrToStringAuto( GetCommandLine() );
    }
  }

getstartupdirectory_test.cs

Synopsis
using System;
using ut;

public class test_getcurrentdirectory
{
  string mycmdline;
  private string testcmdline()
  {
    return mycmdline;
  }

  //missing tests:
  //  if the .exe is in the PATH and it 
  //  is started from a directory other than from that directory
  public test_getcurrentdirectory()
  {
    getstartupdirectory.func = new getstartupdirectory.getcmdlineDelegate(testcmdline);
  }
  public void test_startupdir_normal()
  {
    mycmdline = @"d:\abc\xyz.exe";
    utx.assert(getstartupdirectory.StartupDirectory, @"d:\abc");
  }

  public void test_startupdir_quoted()
  {
    mycmdline = "\"d:\\abc\\xyz.exe\"";
    utx.assert(getstartupdirectory.StartupDirectory, @"d:\abc");
  }
  public void test_startupdir_simpleparm()
  {
    mycmdline = "\"d:\\abc\\xyz.exe\" xyw";
    utx.assert(getstartupdirectory.StartupDirectory, @"d:\abc");
  }
  public void test_startupdir_parmisapath()
  {
    mycmdline = "d:\\abc\\xyz.exe d:\\xyw";
    utx.assert(getstartupdirectory.StartupDirectory, @"d:\abc");
  }
  public void test_startupdir_quoted_parmisapath()
  {
    mycmdline = "\"d:\\abc\\xyz.exe\" d:\\xyw";
    utx.assert(getstartupdirectory.StartupDirectory, @"d:\abc");
  }
  public void test_startupdir_quoted_embeddedspace_parmisapath()
  {
    mycmdline = "\"d:\\abc def\\xyz.exe\" d:\\xyw";
    utx.assert(getstartupdirectory.StartupDirectory, @"d:\abc def");
  }
  public void test_startupdir_quoted_embeddedspace2_parmisapath()
  {
    mycmdline = "\"d:\\abc  def ghi\\xyz.exe\" d:\\xyw";
    utx.assert(getstartupdirectory.StartupDirectory, @"d:\abc  def ghi");
  }
}






Contact me about content on this page using john_web-at-arrizza-dot-com
For Web Master or site problems contact: webadmin-at-arrizza-dot-com
Copyright John Arrizza (c) 2001-2010