[英]Relative path to a file using C#
I have a command-line program that takes a configuration file as a parameter, that is, C:\\myprogram.exe -c C:\\configFiles\\MyConfigFile.txt . 我有一个命令行程序,它将配置文件作为参数,即C:\\ myprogram.exe -c C:\\ configFiles \\ MyConfigFile.txt 。 I want to be able to make it so that I do not have to type the absolute path to the configuration file, so instead of the above I can just enter C:\\myprogram.exe -c MyConfigFile.txt 我希望能够这样做,这样我就不必输入配置文件的绝对路径,所以我可以输入C:\\ myprogram.exe -c MyConfigFile.txt而不是上面的命令。
I'm not familiar enough with how C# deals with paths to understand if or how this can be done. 我不太熟悉C#如何处理路径以了解是否或如何做到这一点。 Any insight is appreciated. 任何见解都表示赞赏。
Use Path.GetFullPath() 使用Path.GetFullPath()
Something like: 就像是:
string path = "MyConfigFile.txt";
string fullPath = System.IO.Path.GetFullPath(path);
That's if the config file is in the current directory. 如果配置文件在当前目录中。 If they're stored in a standard folder you can use Path.Combine() 如果它们存储在标准文件夹中,您可以使用Path.Combine()
string basePath = "C:\Configs";
string file = "MyConfigFile.txt";
string fullPath = System.IO.Path.Combine(basePath, file);
You are only passing in a string. 你只传递一个字符串。 It is up to you to handle it in a relative path way. 你应该以相对路径的方式来处理它。
If you used this code, it would by default support relative paths. 如果您使用此代码,它将默认支持相对路径。
FileInfo
will attempt to use the path in Environment.CurrentDirectory
first, if the path provided is not explicit. 如果提供的路径不明确, FileInfo
将首先尝试使用Environment.CurrentDirectory
的路径。
var file = new FileInfo(args[1]); // args[1] can be a filename of a local file
Console.WriteLine(file.FullName); // Would show the full path to the file
You have some general options: 你有一些一般选择:
Any way you slice it, the user must indicate the path to the directory, be it in the config file, entering relative path, or selecting from a list of files with identical names found in different directories. 无论如何切片,用户必须指明目录的路径,无论是在配置文件中,输入相对路径,还是从不同目录中具有相同名称的文件列表中进行选择。
ANALOGY: You place a textbook in a locker, somewhere in a school. 类比:你把一本教科书放在学校里的某个储物柜里。 You ask your friend to get the book from "a locker", without hinting as to exactly which locker that may be. 你要求你的朋友从“储物柜”中取出这本书,而不是暗示可能是哪个储物柜。 You should expect to wait a very long time and end up with 50 similar books. 你应该等待很长时间,并最终得到50本类似的书。 Or, provide a clue as to the hallway, bank of lockers, and/or locker number in which the book may be stored. 或者,提供关于可以存储书籍的走廊,储物柜组和/或储物柜号码的线索。 The vagueness of your clue is directly correlated with the response time and potential for returning multiple possibilities. 线索的模糊性与响应时间和返回多种可能性的可能性直接相关。
Well, standard .NET classes are working with relative paths in same way as with absolute, so, you could use them without any problems. 好吧,标准.NET类使用相对路径和绝对路径一样,因此,您可以毫无问题地使用它们。 If C: root is working directory of your program, you could use c:\\myprogram.exe -c configFiles\\MyConfigFile.txt
and it will point to c:\\configFiles\\MyConfigFile.txt
. 如果C:root是程序的工作目录,则可以使用c:\\myprogram.exe -c configFiles\\MyConfigFile.txt
,它将指向c:\\configFiles\\MyConfigFile.txt
。
Without the relative path as part of the input argument, your application would need to: 如果没有相对路径作为输入参数的一部分,您的应用程序将需要:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.