• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
张纯睿
博客园    首页    新随笔    联系   管理    订阅  订阅

相对路径使用方法

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

posted @ 2012-01-09 11:24  张纯睿  阅读(344)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3