I have a question that I have no idea how to ask in a simple way so I will do and introduction fist and the question at the end I hope that you get the idea
Using RemoteFileInfo C# class you get the files in the location and two additional records parent directory and current directory.
Searching on “/” I will get:
/.. parent directory
/. Current directory
/Test file folder fullname
/Test2 file folder fullname
If in the same Session I move forward to the Test folder using “/Test” I get:
/TEST/.. parent directory
/TEST/. Current directory
/TEST/New folder file folder full name
/TEST/New folder2 file folder full name
If in the same Session I go back to the previous folder using the parent directory “/TEST/..” I get:
/TEST/./../.. parent directory
/TEST/./../. current directory
/TEST/./../.. file folder full name
/TEST2/./../.. file folder full name
If in the same Session I go forward again to Test Folder using the test folder full name “/TEST/./../TEST” that I get on the previous result I get:
/TEST/./../TEST/.. parent directory
/TEST/./../TEST/. current directory
/TEST/./../TEST/New folder
/TEST/./../TEST/New folder2
If I keep back and forward in the same Session using the parent fullname or the file folder fullname it works it moves to the right location but each time the fullname keep growing with the previous fullname on it. if there a way I can get a single path when moving back and forward? Like when I go to the “/” after moving back and forward I get:
/.. parent directory
/. Current directory
/Test file folder full name
/Test2 file folder full name
I’m doing a single file explorer interface and I want to show the user the current path but is confusing showing something like this /TEST/./../TEST/New folder as current location.
Please let me know if better explanation is need it ;)
I made Minimal Reproducible Example using C# Console Application.
class Program
{
static Session session = new Session();
static void Main(string[] args)
{
var l = new List<DirectoryInformation>();
string GoToThisDirectory = "";
//OPEN SESSION
SessionIni();
//Get File information for the Root folder
Console.WriteLine("ROOT FOLDER");
l = GetDirFiles("/TEST");
foreach (var item in l)
{
// let's take the fist directory that is not this directory to move to that one next time
if (item.IsThisDirectory == false)
{
GoToThisDirectory = item.FullName;
}
//Display the current directory
if (item.IsThisDirectory == true)
{
Console.WriteLine(string.Concat("CURRENT FOLDER: ", item.FullName));
}
Console.WriteLine(string.Concat(" FullName: ", item.FullName,
" IsDirectory: ", item.IsDirectory,
" IsThisDirectory: ", item.IsThisDirectory,
" IsParentDirectory: ", item.IsParentDirectory));
}
Console.WriteLine();
l = GetDirFiles(GoToThisDirectory);
Console.WriteLine("MOVE FORWARD TO LEVEL1 FOLDER");
foreach (var item in l)
{
// let's take the parent directory folder to move to that one next time
if (item.IsParentDirectory == true)
{
GoToThisDirectory = item.FullName;
}
if (item.IsThisDirectory == true)
{
Console.WriteLine(string.Concat("CURRENT FOLDER: ", item.FullName));
}
Console.WriteLine(string.Concat(" FullName: ", item.FullName,
" IsDirectory: ", item.IsDirectory,
" IsThisDirectory: ", item.IsThisDirectory,
" IsParentDirectory: ", item.IsParentDirectory));
}
Console.WriteLine();
l = GetDirFiles(GoToThisDirectory);
Console.WriteLine("MOVE BACK TO PARENT DIRECTORY FOLDER");
foreach (var item in l)
{
// let's take the fist directory that is not this directory to move to that one again
if (item.IsThisDirectory == false)
{
GoToThisDirectory = item.FullName;
}
if (item.IsThisDirectory == true)
{
Console.WriteLine(string.Concat("CURRENT FOLDER: ", item.FullName));
}
Console.WriteLine(string.Concat(" FullName: ", item.FullName,
" IsDirectory: ", item.IsDirectory,
" IsThisDirectory: ", item.IsThisDirectory,
" IsParentDirectory: ", item.IsParentDirectory));
}
Console.WriteLine();
l = GetDirFiles(GoToThisDirectory);
Console.WriteLine("MOVE BACK AGAIN TO LEVEL1 FOLDER");
foreach (var item in l)
{
if (item.IsThisDirectory == true)
{
Console.WriteLine(string.Concat("CURRENT FOLDER: ", item.FullName));
}
Console.WriteLine(string.Concat(" FullName: ", item.FullName,
" IsDirectory: ", item.IsDirectory,
" IsThisDirectory: ", item.IsThisDirectory,
" IsParentDirectory: ", item.IsParentDirectory));
}
Console.Read();
}
private static void SessionIni()
{
SessionOptions sessionOptions = new SessionOptions
{
Protocol = Protocol.Sftp,
HostName = "XX.XX.XX.XX",
UserName = "UserName",
Password = "Password",
SshHostKeyFingerprint = "XXXXXXXXXXXXXXX",
};
session.Open(sessionOptions);
}
private static List<DirectoryInformation> GetDirFiles(string dir)
{
var l = new List<DirectoryInformation>();
RemoteDirectoryInfo directory = session.ListDirectory(dir);
foreach (RemoteFileInfo fileInfo in directory.Files)
{
l.Add(new DirectoryInformation
{
Name = fileInfo.Name,
FullName = fileInfo.FullName,
IsDirectory = fileInfo.IsDirectory,
IsThisDirectory = fileInfo.IsThisDirectory,
IsParentDirectory = fileInfo.IsParentDirectory,
});
}
return l;
}
}
class DirectoryInformation
{
public string Name { get; set; }
public string FullName { get; set; }
public bool IsDirectory { get; set; }
public bool IsThisDirectory { get; set; }
public bool IsParentDirectory { get; set; }
}
Console Read:
ROOT FOLDER
CURRENT FOLDER: /TEST/.
FullName: /TEST/. IsDirectory: True IsThisDirectory: True IsParentDirectory: False
FullName: /TEST/.. IsDirectory: True IsThisDirectory: False IsParentDirectory: True
FullName: /TEST/Level1 IsDirectory: True IsThisDirectory: False IsParentDirectory: False
MOVE FORWARD TO LEVEL1 FOLDER
CURRENT FOLDER: /TEST/Level1/.
FullName: /TEST/Level1/. IsDirectory: True IsThisDirectory: True IsParentDirectory: False
FullName: /TEST/Level1/.. IsDirectory: True IsThisDirectory: False IsParentDirectory: True
FullName: /TEST/Level1/Level2 IsDirectory: True IsThisDirectory: False IsParentDirectory: False
MOVE BACK TO PARENT DIRECTORY FOLDER
CURRENT FOLDER: /TEST/Level1/../.
FullName: /TEST/Level1/../. IsDirectory: True IsThisDirectory: True IsParentDirectory: False
FullName: /TEST/Level1/../.. IsDirectory: True IsThisDirectory: False IsParentDirectory: True
FullName: /TEST/Level1/../Level1 IsDirectory: True IsThisDirectory: False IsParentDirectory: False
MOVE BACK AGAIN TO LEVEL1 FOLDER
CURRENT FOLDER: /TEST/Level1/../Level1/.
FullName: /TEST/Level1/../Level1/. IsDirectory: True IsThisDirectory: True IsParentDirectory: False
FullName: /TEST/Level1/../Level1/.. IsDirectory: True IsThisDirectory: False IsParentDirectory: True
FullName: /TEST/Level1/../Level1/Level1 IsDirectory: True IsThisDirectory: False IsParentDirectory: False
The last current forlder shows:
CURRENT FOLDER: /TEST/Level1/../Level1/.
and I wish to get
/TEST/Level1/