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

c获得文件路径|c语言如何获取用户通过键盘输入的文件目录中的文件名和文件路径ballball大佬帮帮我🙏求代码的第1张示图

A. 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\

B. 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. 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一样。

D. objective-c如何获得文件路径

方法:通过NSHomeDirectory获得文件路径代码如下:NSString *homeDirectory = NSHomeDirectory();NSString *fileDirectory = [homeDirectory :@"temp/app_data.plist"];1.//使用检索指定路径NSArray *path = (NSDocumentDirectory, NSUserDomainMask, YES);NSString *documentsDirectory = [paths objectAtIndex:0];zNSString *fileDirectory = [documentsDirectory :@"file.txt"];2.//使用Foundation中的NSTemporaryDirectory函数直接返回代表temp文件夹的全路径的字符串对象NSString *tempDirectory = NSTemporaryDirectory();NSString *file = [tempDirectory :@"file.txt"];实现例子如下:NSArray *path = (NSCachesDirectory, NSUserDomainMask, YES);NSString *docDir = [path objectAtIndex:0];NSLog(@"filepath:%@",docDir);NSString *str = @"hello.jpg";NSString *filepath = [docDir :str];//NSString *filepath = [docDir :[NSString stringWithUTF8String:"///mest.txt"]];NSLog(@"filepath:%@",filepath);BOOL success = [[NSFileManager defaultManager]createFileAtPath: filepath contents:nil attributes:nil];NSLog(@"result",success);printf("Create File:%s %s.",[filepath UTF8String], success ? "Success" : "Error");NSString* reValue= [NSString stringWithString:@"\\"success\\""];

E. c语言怎么获得当前程序运行路径

main参数啊! int main(int argc,char *argv[]) { printf(argv[0]); return 0; } 这样得到一个字符串,从中提取出路径不是难事,如果不行Q960575562

F. 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

G. 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;}

H. C语言…如何获取进程的可执行文件路径…

hMole=CreateToolhelp32Snapshot(TH32CS_SNAPMODULE,pinfo->th32ProcessID); Mole32First(hMole, minfo); GetShortPathName(minfo->szExePath,shortpath,256);如果还需要详细的代码,请Q我吧。。我最近刚在做这块。。。新手,写的东西比较简单- –

I. 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语言如何获取用户通过键盘输入的文件目录中的文件名和文件路径ballball大佬帮帮我🙏求代码

赞 (0)