Как узнать программно имя своего экзешника?

Тема в разделе ".NET", создана пользователем e-Pasha, 8 сен 2006.

Статус темы:
  1. e-Pasha

    e-Pasha Гость

    Как узнать программно имя своего экзешника? Запускается программа, и она должна определить свое имя.
  2. Partisan

    Partisan Гость

    По-моему, это нулевой параметр в параметрах командной строки.
  3. e-Pasha

    e-Pasha Гость

    в смысле args[0]? Та не.
  4. Partisan

    Partisan Гость



    The GetModuleFileName function retrieves the fully-qualified path for the file that contains the specified module that the current process owns.

    GetModuleFileName operates only on modules that the current process owns. To specify modules that belong to another process, use the GetModuleFileNameEx function.

    DWORD GetModuleFileName(
    HMODULE hModule,
    LPTSTR lpFilename,
    DWORD nSize

    [in] Handle to the module whose path is being requested. If this parameter is NULL, GetModuleFileName retrieves the path of the executable file of the current process.
    [out] Pointer to a buffer that receives a null-terminated string that specifies the fully-qualified path of the module. If the length of the path exceeds the size that the nSize parameter specifies, the function succeeds, and the string is truncated to nSize characters and cannot be null terminated.
    The string returned will use the same format that was specified when the module was loaded. Therefore, the path can be a long or short file name, and can use the prefix "\\?\". For more information, see Naming a File.

    To separate the path from the file name and extension, use the PathRemoveFileSpec function. Note that this function does not support paths with the "\\?\" prefix.

    [in] Size of the lpFilename buffer, in TCHARs.
    Return Values
    If the function succeeds, the return value is the length of the string that is copied to the buffer, in TCHARs. If the buffer is too small to hold the module name, the string is truncated to nSize, and the function returns nSize.

    If the function fails, the return value is 0 (zero). To get extended error information, call GetLastError.

    If a DLL is loaded in two processes, its file name in one process may differ in case from its file name in the other process.

    For the ANSI version of the function, the number of TCHARs is the number of bytes; for the Unicode version, it is the number of characters.

    The global variable _pgmptr is automatically initialized to the full path of the executable file, and can be used to retrieve the full path name of an executable file.

    Windows Me/98/95: This function retrieves long file names when an application version number is greater than or equal to 4.00 and the long file name is available. Otherwise, it returns only 8.3 format file names.
    Example Code
    For an example, see Installing a Service.

    Client Requires Windows XP, Windows 2000 Professional, Windows NT Workstation, Windows Me, Windows 98, or Windows 95.
    Server Requires Windows Server 2003, Windows 2000 Server, or Windows NT Server.
    Header Declared in Winbase.h; include Windows.h.

    Library Link to Kernel32.lib.

    DLL Requires Kernel32.dll.
    Unicode Implemented as GetModuleFileNameW (Unicode) and GetModuleFileNameA (ANSI). Note that Unicode support on Windows Me/98/95 requires Microsoft Layer for Unicode.

    See Also
    Dynamic-Link Library Functions, GetModuleFileNameEx, GetModuleHandle, LoadLibrary
  5. DZX

    DZX Гость

    (WinForms) Application.ExecutablePath
    Это командная строка из нее получить имя файла не сложно
  6. Little Friend

    Little Friend Гость

    args[0] тоже верно
  7. Pasha

    Pasha Гость

    args[0] не совсем корректно для .NET. 2005-я студия любит запускать exe не напрямую, а через всякие прокси типа ConsoleApplication1.vshost.exe.

    если полный путь, то:
    если только имя файла, то:
Статус темы:

Поделиться этой страницей