c语言获取当前项目路径的文件

c语言获取当前项目路径的文件,第1张

/*从字符串的左边截取n个字符*/

char * left(char *dst,char *src, long n)

{

    char *p = src

    char *q = dst

    int len = strlen(src)

    if(n>len) n = len

    /*p += (len-n)*/  /*从右边第n个字符开始*/

    while(n--) *(q++) = *(p++)

    *(q++)='\0'/*有必要吗?很有必要*/

    return dst

}

int substr(char *s1, char *s2)

{

    char *s3 = strstr(s1,s2)

    if(s3 == NULL)

          return -1

    return strlen(s1)-strlen(s3)

}

char* mysubstr(char* srcstr, int offset, int length) {

    int total_length = strlen(srcstr)

    int real_length = ((total_length - offset) >= length ? length : (total_length - offset)) + 1

    char *tmp

    if (NULL == (tmp=(char*) malloc(real_length * sizeof(char)))) {

        printf("Memory overflow . \n")

        exit(0)

    }

    strncpy(tmp, srcstr+offset, real_length - 1)

    tmp[real_length - 1] = '\0'

    return tmp

}

#define ENCODE 1

#define IN_PCM(a) strcat(a,"/file/input.pcm")

#define OUT_SPX(a) strcat(a,"/file/out.spx")

#define OUT_PCM(a) strcat(a,"/file/out.pcm")

int main(int argc, char **argv){

    char* path

    char* dist

    path = argv[0]

    char* index =  strstr(path,PROJECT_PATH)

    long diff = index-path

    char *pathDir = mysubstr(path,0,substr(path,PROJECT_PATH)+9)

    // 创建一个字符串数组

    char arr1[64] = {0}

    strcpy(arr1, pathDir)

    char arr2[64] = {0}

    strcpy(arr2, pathDir)

    char* temp0=IN_PCM(pathDir)

    char* temp=OUT_SPX(arr1)

    char* tempOut=OUT_PCM(arr2)

    // 创建一个字符串数组

    char arr0[64] = {0}

    strcpy(arr0, temp0)

    printf("after arr0=%s\n,arr1=%s\n,arr2=%s\n",arr0,arr1,arr2)

    return 0

}

http://hi.baidu.com/andywangcn/item/7633efda5517baf9ca0c39c6

获得双斜杠路径不包含文件名

TCHAR _szPath[MAX_PATH + 1]={0}

GetModuleFileName(NULL, _szPath, MAX_PATH)

(_tcsrchr(_szPath, _T('\\')))[1] = 0//删除文件名,只获得路径 字串

CString strPath

for (int n=0_szPath[n]n++)

{

if (_szPath[n]!=_T('\\'))

{

strPath +=_szPath[n]

}

else

{

strPath += _T("\\\\")

}

}

MessageBox(strPath)//输出==e:\\program\\Debug\\

//头文件用到 windows.h

C#获取当前应用程序所在路径及环境变量

一、获取当前文件的路径

string str1=Process.GetCurrentProcess().MainModule.FileName//可获得当前执行的exe的文件名。

string str2=Environment.CurrentDirectory//获取和设置当前目录(即该进程从中启动的目录)的完全限定路径。(备注:按照定义,如果该进程在本地或网络驱动器的根目录中启动,则此属性的值为驱动器名称后跟一个尾部反斜杠(如“C:\”)。如果该进程在子目录中启动,则此属性的值为不带尾部反斜杠的驱动器和子目录路径[如“C:\mySubDirectory”])。

string str3=Directory.GetCurrentDirectory()//获取应用程序的当前工作目录。

string str4=AppDomain.CurrentDomain.BaseDirectory//获取基目录,它由程序集冲突解决程序用来探测程序集。

string str5=Application.StartupPath//获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。

string str6=Application.ExecutablePath//获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。

string str7=AppDomain.CurrentDomain.SetupInformation.ApplicationBase//获取或设置包含该应用程序的目录的名称。

1. System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName

获取模块的完整路径。

2. System.Environment.CurrentDirectory

获取和设置当前目录(该进程从中启动的目录)的完全限定目录。

3. System.IO.Directory.GetCurrentDirectory()

获取应用程序的当前工作目录。这个不一定是程序从中启动的目录啊,有可能程序放在C:\www里,这个函数有可能返回C:\Documents and Settings\ZYB\,或者C:\Program Files\Adobe\,有时不一定返回什么东东,这是任何应用程序最后一次 *** 作过的目录,比如你用Word打开了E:\doc\my.doc这个文件,此时执行这个方法就返回了E:\doc了。

4. System.AppDomain.CurrentDomain.BaseDirectory

获取程序的基目录。

5. System.Windows.Forms.Application.StartupPath

获取启动了应用程序的可执行文件的路径。效果和2、5一样。只是5返回的字符串后面多了一个"\"而已。

6. System.Windows.Forms.Application.ExecutablePath

获取启动了应用程序的可执行文件的路径及文件名,效果和1一样。

7. System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase

获取和设置包括该应用程序的目录的名称。

二、 *** 作环境变量

利用System.Environment.GetEnvironmentVariable()方法可以很方便地取得系统环境变量,如:System.Environment.GetEnvironmentVariable("windir")就可以取得windows系统目录的路径。

以下是一些常用的环境变量取值:

System.Environment.GetEnvironmentVariable("windir")

System.Environment.GetEnvironmentVariable("INCLUDE")

System.Environment.GetEnvironmentVariable("TMP")

System.Environment.GetEnvironmentVariable("TEMP")

System.Environment.GetEnvironmentVariable("Path")

三、应用实例

编写了一个WinForm程序,项目文件存放于D:\Projects\Demo,编译后的文件位于D:\Projects\Demo\bin\Debug,最后的结果如下:

1、System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName=D:\Projects\Demo\bin\Debug\Demo.vshost.exe

2、System.Environment.CurrentDirectory=D:\Projects\Demo\bin\Debug

3、System.IO.Directory.GetCurrentDirectory()=D:\Projects\Demo\bin\Debug

4、System.AppDomain.CurrentDomain.BaseDirectory=D:\Projects\Demo\bin\Debug\

5、System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase=D:\Projects\Demo\bin\Debug\

6、System.Windows.Forms.Application.StartupPath=D:\Projects\Demo\bin\Debug

7、System.Windows.Forms.Application.ExecutablePath=D:\Projects\Demo\bin\Debug\Demo.EXE

System.Environment.GetEnvironmentVariable("windir")=C:\WINDOWS

System.Environment.GetEnvironmentVariable("INCLUDE")=C:\Program Files\Microsoft Visual Studio.NET 2005\SDK\v2.0\include\

System.Environment.GetEnvironmentVariable("TMP")=C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp

System.Environment.GetEnvironmentVariable("TEMP")=C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp

System.Environment.GetEnvironmentVariable("Path")=C:\WINDOWS\system32C:\WINDOWSC:\WINDOWS\System32\WbemC:\Program Files\Microsoft SQL Server\90\Tools\binn\


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/tougao/11463480.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-16
下一篇 2023-05-16

发表评论

登录后才能评论

评论列表(0条)

保存