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() );
}
}
|
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");
}
}
|