相对路径使用方法

c++
// 没有盘符,为相对路径
strDllPath.Trim(_T("\\"));
strDllPath = CEnvironment::ConfigAppDir()+strDllPath;

c#
String strDir = Environment.CommandLine.Substring(0, Environment.CommandLine.LastIndexOf(@"\XQ-4."));
或者
Environment.CurrentDirectory // 当操作中途有使用文件打开对话框的情况时,该函数可能会发生变化

《Environment.h》

#pragma once
class CEnvironment
{
public:
CEnvironment(void);
~CEnvironment(void);
static CString ConfigAppDir(void);
static int Init(void);
static CString m_AppPath;
static CString m_AppDriver;
static CString m_AppDir;
static CString m_AppFileName;
static CString m_AppFileExt;

static void OutputMsg01(CString tag , CString str , BOOL clean);
};

《Environment.cpp》

#include "StdAfx.h"
#include "Environment.h"

CString CEnvironment::m_AppPath = _T("");
CString CEnvironment::m_AppDriver = _T("");
CString CEnvironment::m_AppDir = _T("");
CString CEnvironment::m_AppFileName = _T("");
CString CEnvironment::m_AppFileExt = _T("");

CEnvironment::CEnvironment(void)
{
}


CEnvironment::~CEnvironment(void)
{
}


int CEnvironment::Init(void)
{
if (_T("")!=m_AppDriver)
return 0;

TCHAR path[MAX_PATH];
memset(path,0,sizeof(TCHAR)*MAX_PATH);
GetModuleFileName( NULL,path, MAX_PATH );

TCHAR drive[MAX_PATH],dir[MAX_PATH],fname[MAX_PATH],ext[MAX_PATH];
_tsplitpath(path,drive,dir,fname,ext );

m_AppDriver = drive;
m_AppDir = dir;
m_AppFileName = fname;
m_AppFileExt = ext;
//m_AppPath = drive;

return 1;
}

CString CEnvironment::ConfigAppDir(void)
{
Init();
return m_AppDriver+m_AppDir;
//m_AppPath = drive;
//m_AppPath += dir;
}


void CEnvironment::OutputMsg01(CString tag , CString str , BOOL clean)
{
CString strFilePath = _T("c:\\output111.txt");
CStdioFile file;
BOOL res = FALSE;
if (clean)
{
res = file.Open(strFilePath, CFile::modeCreate|CFile::modeReadWrite);
}
else
{
res = file.Open(strFilePath, CFile::modeCreate|CFile::modeNoTruncate|CFile::modeReadWrite);
}

if (res)
{
if (!clean)
{
file.SeekToEnd();
}

if (_T("")!=tag)
{
file.WriteString(tag);
file.WriteString(_T("\r\n"));
}

if (_T("")!=str)
{
file.WriteString(str);
file.WriteString(_T("\r\n"));
}

file.Close();
}
}

原文地址:https://www.cnblogs.com/carl2380/p/2316916.html