Relative file path using C #

I have a command line program that takes a configuration file as a parameter, i.e. C: \ myprogram.exe -c C: \ configFiles \ MyConfigFile.txt . I want to be able to do this so that I do not need to enter an absolute path to the configuration file, so instead of the above, I can simply enter C: \ myprogram.exe -c MyConfigFile.txt

I'm not good enough at how C # deals with paths to understand how and how to do this. Any insight appreciated.

+8
c # programming-languages
source share
5 answers

Use Path.GetFullPath ()

Something like:

string path = "MyConfigFile.txt"; string fullPath = System.IO.Path.GetFullPath(path); 

This is if the configuration file is in the current directory. If they are stored in a standard folder, you can use Path.Combine ()

 string basePath = "C:\Configs"; string file = "MyConfigFile.txt"; string fullPath = System.IO.Path.Combine(basePath, file); 
+12
source share

You only pass a string. It is up to you to handle it along a relative path.

If you used this code, it will support relative paths by default.

FileInfo will first try to use the path in Environment.CurrentDirectory if the path specified is not explicit.

 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 
+4
source share

You have common options:

  • Save the directory path in the configuration file (you said you did not want to do this)
  • Ask the user to enter a relative path (.... \ MyFile.txt)
  • Programmatically searching for files matching the name (very slow and prone to finding multiple files with the same name)
  • Suppose the data file is in the executing directory

In any case, you cut it, the user must specify the path to the directory, whether in the configuration file, enter the relative path or select from the list of files with the same names in different directories.

ANALOGUE: You place a textbook in a locker, somewhere in a school. You ask your friend to take the book from the โ€œlockerโ€, not hinting what kind of locker may be. You have to wait a very long time and eventually get 50 similar books. Or indicate the key to the entrance hall, the locker bank and / or the locker number in which the book can be stored. The uncertainty of your prompt directly correlates with the response time and the potential for returning several possibilities.

+2
source share

Well, the standard .NET classes work with relative paths in exactly the same way as with absolute paths, so you can use them without any problems. If C: root is the working directory of your program, you can use c:\myprogram.exe -c configFiles\MyConfigFile.txt and it will point to c:\configFiles\MyConfigFile.txt .

0
source share

Without a relative path as part of the input argument, your application would have to:

  • either find the file that is being transferred to
  • add path name to Config directory to file name
0
source share

Source: https://habr.com/ru/post/651261/


All Articles