c取文件路径|c语言中如何得到当前文件所在位置

c取文件路径|c语言中如何得到当前文件所在位置的第1张示图

❶ C#如何实现获得一个文件夹的路径

总结C#获取当前路径的7种方法 C#获取当前路径的方法如下: 1. System.Diagnostics.Process.GetCurrentProcess().MainMole.FileName -获取模块的完整路径。 2. System.Environment.CurrentDirectory -获取和设置当前目录(该进程从中启动的目录)的完全限定目录。 3. System.IO.Directory.GetCurrentDirectory() -获取应用程序的当前工作目录。这个不一定是程序从中启动的目录啊,有可能程序放在C:\www里,这个函数有可能返回C:\Documents and Settings\ZYB\,或者C:\Program Files\Adobe\,有时不一定返回什么东东,我也搞不懂了。 4. System.AppDomain.CurrentDomain.BaseDirectory -获取程序的基目录。 5. System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase -获取和设置包括该应用程序的目录的名称。 6. System.Windows.Forms.Application.StartupPath -获取启动了应用程序的可执行文件的路径。效果和2、5一样。只是5返回的字符串后面多了一个"\"而已 7. System.Windows.Forms.Application.ExecutablePath -获取启动了应用程序的可执行文件的路径及文件名,效果和1一样。

❷ c语言如何获取用户通过键盘输入的文件目录中的文件名和文件路径,ballball大佬帮帮我🙏求代码

int main(){string s = "c:\\abc\\def\\text.txt";int xie_index = s.find_last_of('\\');制// 路径中最后一个\的位置 string file_dirname = s.substr(0, xie_index + 1);string file_basename = s.substr(xie_index + 1, s.size());cout << file_dirname << endl << file_basename << endl;}

❸ c#怎么获取程序当前运行路径

C#获取当前应用程序所在路径及环境变量一、获取当前文件的路径string str1=Process.GetCurrentProcess().MainMole.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().MainMole.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().MainMole.FileName=D:\Projects\Demo\bin\Debug\Demo.vshost.exe2、System.Environment.CurrentDirectory=D:\Projects\Demo\bin\Debug3、System.IO.Directory.GetCurrentDirectory()=D:\Projects\Demo\bin\Debug4、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\Debug7、System.Windows.Forms.Application.ExecutablePath=D:\Projects\Demo\bin\Debug\Demo.EXESystem.Environment.GetEnvironmentVariable("windir")=C:\WINDOWSSystem.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\TempSystem.Environment.GetEnvironmentVariable("TEMP")=C:\DOCUME~1\ADMINI~1\LOCALS~1\TempSystem.Environment.GetEnvironmentVariable("Path")=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;C:\Program Files\Microsoft SQL Server\90\Tools\binn\

❹ c语言如何获得文件当前路径

C语言里在main函数来的第二个参数里面,自保存着当前程序运行的目录也就是argv[0]main( int argc, char *argv[]){ printf("%s ", argv[0] );}就是文件当前所在位置不过需要注意的一点是这个路径里面保存了当前文件的文件名如果你只是需要路径的话还需要自己操作一下main(int a,char *c[]){ char s[100]; int i; //把路径保存到字符串s里 strcpy(s,c[0]); for(i=strlen(s); i>0 ; i–) if( s[i] == '\\') { s[i]='\0'; break; } //找到最后一个 \ 并删除之后的内容 //最后输出的s,就是当前文件的路径了 puts(s);}

❺ c语言读取文件的路径怎么设定

//获取指定目录下的所有文件列表 author:wangchangshaui jlu char** getFileNameArray(const char *path, int* fileCount) { int count = 0; char **fileNameList = NULL; struct dirent* ent = NULL; DIR *pDir; char dir[512]; struct stat statbuf; //打开目录 if ((pDir = opendir(path)) == NULL) { myLog("Cannot open directory:%s\n", path); return NULL; } //读取目录 while ((ent = readdir(pDir)) != NULL) { //统计当前文件夹下有多少文件(不包括文件夹) //得到读取文件的绝对路径名 snprintf(dir, 512, "%s/%s", path, ent->d_name); //得到文件信息 lstat(dir, &statbuf); //判断是目录还是文件 if (!S_ISDIR(statbuf.st_mode)) { count++; } } //while //关闭目录 closedir(pDir); // myLog("共%d个文件\n", count); //开辟字符指针数组,用于下一步的开辟容纳文件名字符串的空间 if ((fileNameList = (char**) myMalloc(sizeof(char*) * count)) == NULL) { myLog("Malloc heap failed!\n"); return NULL; } //打开目录 if ((pDir = opendir(path)) == NULL) { myLog("Cannot open directory:%s\n", path); return NULL; } //读取目录 int i; for (i = 0; (ent = readdir(pDir)) != NULL && i < count;) { if (strlen(ent->d_name) <= 0) { continue; } //得到读取文件的绝对路径名 snprintf(dir, 512, "%s/%s", path, ent->d_name); //得到文件信息 lstat(dir, &statbuf); //判断是目录还是文件 if (!S_ISDIR(statbuf.st_mode)) { if ((fileNameList[i] = (char*) myMalloc(strlen(ent->d_name) + 1)) == NULL) { myLog("Malloc heap failed!\n"); return NULL; } memset(fileNameList[i], 0, strlen(ent->d_name) + 1); strcpy(fileNameList[i], ent->d_name); myLog("第%d个文件:%s\n", i, ent->d_name); i++; } } //for //关闭目录 closedir(pDir); *fileCount = count; return fileNameList

❻ C#获取改文件的对路径应该调用什么方法

Path.getfilename()

❼ c#怎么获取一个文件的相对路径

获取当前文件的相对路径:1. System.Diagnostics.Process.GetCurrentProcess().MainMole.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.AppDomain.CurrentDomain.SetupInformation.ApplicationBase 获取和设置包括该应用程序的目录的名称。6. System.Windows.Forms.Application.StartupPath 获取启动了应用程序的可执行文件的路径。效果和2、5一样。只是5返回的字符串后面多了一个"\"而已7. System.Windows.Forms.Application.ExecutablePath 获取启动了应用程序的可执行文件的路径及文件名,效果和1一样。

❽ c语言中如何得到当前文件所在位置

如果是通过open方式打开的,那么第一个参数就是文件路径信息:#include <sys/types.h>#include <sys/stat.h>#include <fcntl.h>int open(const char *path, int oflag, /* mode_t mode */…);如果是通过fopen方式打开的,那么第一个参数就是文件路径信息:#include <stdio.h>FILE *fopen(const char *filename, const char *mode);无论通过open还是fopen打开文件,都必须先知道文件路径信息,尽管可能是相对路径。如果知道了filename的内容,我们就可以定位它的绝对路径,也就是你说的完全路径。1. filename本身就是绝对路径,ok。2. filename是相对路径,那么先通过getcwd获取进程的执行路径,然后再获取绝对路径即可。#include <unistd.h>extern char *getcwd(char *buf, size_t size);但是,如果进程在打开文件后又执行了chdir、fchdir之类函数的话,估计就不能够再获取文件路径信息了。#include <unistd.h>int chdir(const char *path);int fchdir(int fildes);

❾ C语言试题 编写一个获取文件路径的函数

#include<stdio.h>main(){charstr[80]="d:\\files\\dataFiles\\data\\avatar.bmp";//单斜杠处填双斜杠intL,i;L=strlen(str);for(i=L-1;i>=0;i–)if(str[i]=='\\'){str[i]='\0';break;};//去掉文件名就是路径printf("Path=%s",str);return0;}———–写成函数和调用:#include<stdio.h>voidfun(char*str){intL,i;L=strlen(str);for(i=L-1;i>=0;i–)if(str[i]=='\\'){str[i]='\0';break;};}main(){charstr[80]="d:\\files\\dataFiles\\data\\avatar.bmp";fun(str);printf("Path=%s",str);return0;}

❿ c如何获得FILE*的路径

关于通过 C 语言编程获取到某个文件的完整路径、以及文件名称的问题,那是毫无疑问可内以做容到的。但是具体的编程我由于已经好多年没有编写 C 语言程序了。故具体的程序调试过程需要你自己进行完成了。关于这部分的问题,我记得 C 语言库函数大全上面肯定是有的,你可以参考有关 C 语言库函数的教材。但是注意一点就是:如果是在 WINDOWS 系统下面的编程,那么路径名必须使用双斜线,即:\\ 才行。例如:若想表示打开驱动器 D 上的 MY_SUBDIR 子目录下面的 MYFILE.TXT 文件,则在 C 语言源程序中要写为如下代码:#include <stdio.h>void main( ){FILE * fpr ;fpr = fopen("D:\\MY_SUBDIR\\MYFILE.TXT", "r") ; /* 这样写才是正确的语句。*/……fclose(fpr) ;}

未经允许不得转载:山九号 » c取文件路径|c语言中如何得到当前文件所在位置

赞 (0)