I have this to find the path:

System.IO.FileInfo path = new FileInfo(com.Substring(5));
                        string fullpath = path.FullName;

But that returns the path of the console app + whatever i type.
(e.g. C:\users\RILEY\Desktop\....\....\....\+(com.substring(5)))
What I want to happen is when i enter:

./$ cat log.txt

The console app writes what is in the text file. i can read and display it but i dont want me or my users to to have to write:

C:\(Ungodly long f***ing file path)\(another ungodly long f***ing filepath)\(another ungodly long f***ing filepath)\(another ungodly long f***ing filepath)\(another ungodly long f***ing filepath)\(another ungodly long f***ing filepath)\(another ungodly long f***ing filepath)\(another ungodly long f***ing filepath)\(whatever text file you want to read)

So how do i just type a file name and have it get the full path of it?

Dani AI

Generated

A short, practical fix and why the original behavior happens.

Relative names like log.txt are resolved against the process working directory, so a bare filename will usually expand to the console app’s folder (that’s why echo off saw the long path). As hinted, the working directory matters; as suggested, a GUI OpenFileDialog avoids the problem entirely for interactive users. For a CLI that accepts Unix-style commands, resolve a bare filename by checking an ordered set of locations instead of requiring the user to type an absolute path.

A recommended resolution strategy (in order):

  1. If the user supplies an absolute/rooted path, use it.
  2. Treat an unrooted name as relative: check the current working folder first.
  3. Check the application base folder (where the app lives).
  4. Search a small list of configured directories (from config or an env var) — don’t default to scanning the whole disk.
  5. If still not found, do a limited recursive search with Directory.EnumerateFiles, catching and skipping access errors.

Example resolver (trimmed for clarity):

string ResolveFile(string name, IEnumerable<string> extraDirs)
{
    if (Path.IsPathRooted(name)) return File.Exists(name) ? Path.GetFullPath(name) : null;

    var candidate = Path.GetFullPath(name);               // current dir
    if (File.Exists(candidate)) return candidate;

    candidate = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, name);
    if (File.Exists(candidate)) return Path.GetFullPath(candidate);

    foreach (var dir in extraDirs) {
        var p = Path.Combine(dir, name);
        if (File.Exists(p)) return Path.GetFullPath(p);
    }

    foreach (var dir in extraDirs) {
        try {
            var found = Directory.EnumerateFiles(dir, name, SearchOption.AllDirectories).FirstOrDefault();
            if (found != null) return Path.GetFullPath(found);
        } catch { /* skip dirs we can't read */ }
    }
    return null;
}

Cautions and tips: prefer EnumerateFiles to GetFiles for large trees, limit recursion depth and the set of directories to scan, and catch UnauthorizedAccessException/IO exceptions. Let users add a search path (or use an env/config variable) rather than scanning root drives. Useful docs: Path.GetFullPath, Directory.EnumerateFiles, Path.IsPathRooted, AppDomain.BaseDirectory, File.Exists.

Recommended Answers

All 5 Replies

cat is a Linux, Unix, MSDOS? command. I seem to remember from the old days there exists a $PATH variable you can set.
But I don't know what this has got to do with C# and .NET.
I let my users choose from an Open file dialog, with a few mouse clicks they walk over all their hard disks and choose the file they like. This is the 21st century.
BTW with your attitude of being lazy and getting frustrated because you have to type too much, you are not going to bring it far as a programmer. Better go fishing, or start nitting.

cat is a Linux, Unix, MSDOS? command. I seem to remember from the old days there exists a $PATH variable you can set.
But I don't know what this has got to do with C# and .NET.
I let my users choose from an Open file dialog, with a few mouse clicks they walk over all their hard disks and choose the file they like. This is the 21st century.
BTW with your attitude of being lazy and getting frustrated because you have to type too much, you are not going to bring it far as a programmer. Better go fishing, or start nitting.

I just want it to be user friendly. and writing a paragraph every time you want to open a file is silly when it is not necessary. Yes it is is a linux command but i am making my own commands. Surely it is possible to find a file path with a given file name.

For lazy's eyes only: get working directory

That seems as though i wouldnt give me the file path of...but I can use parts of it to change to the path and then open the file. I ll add that command. Thanks for the help

Have you tried a Long Path Tool? I'm having the same type of problem and it is freaking annoying. I just want a quick fix coz I'm not that tech or computer savvy. Any ideas with pathtoodeep.com's Long Path Tool?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.