0

I'm writting a c# console app that displays a list of items. The user can then navigate through the items and the screen as redrawn appropriately to display the cursor. This runs prefectly within the IDE and when i double click the built .exe. However when I run the .exe via command line everything is displayed incorrectly.

    //Display Current Directory, files and folders and selected file
    public static void Display()
    {
        Console.Clear();

        Console.CursorVisible = false;
            //Sort and combine Directories and files
            files = CombineArrays( currentPath );

            Console.SetCursorPosition( 0, Console.WindowTop + 5 );
            //Output all files and folders
            for (int i = 0; i < files.Length; i++)
            {
                if (Cursor.cursorPosY == i)
                {
                    FileAttributes attr = File.GetAttributes( files[i] );
                    if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
                    {
                        writer.WriteLine( " -> > " + ShortenFileName( Path.GetFileNameWithoutExtension( files[i] ), 26 ) + Path.GetExtension( files[i] ) + "\\" );
                    }
                    else
                    {
                        writer.WriteLine( " -> {0,-40}{1,16}", ShortenFileName( Path.GetFileNameWithoutExtension( files[i] ), 26 )
                            + Path.GetExtension( files[i] ), CalculateFileSize( files[i] ) );
                    }
                }
                else if (Cursor.cursorPosY != i)
                {
                    FileAttributes attr = File.GetAttributes( files[i] );
                    if ((attr & FileAttributes.Directory) == FileAttributes.Directory)
                    {
                        writer.WriteLine( "    > " + ShortenFileName( Path.GetFileNameWithoutExtension( files[i] ), 26 ) + Path.GetExtension( files[i] ) + "\\" );

                    }
                    else
                    {
                        writer.WriteLine( "    {0,-40}{1,16}", ShortenFileName( Path.GetFileNameWithoutExtension( files[i] ), 26 )
                            + Path.GetExtension( files[i] ), CalculateFileSize( files[i] ) );
                    }
                }
            }

        writer.Flush();
        Console.SetCursorPosition( 0, Cursor.cursorPosY );

        //Clear top of window and Write Current Directory
        ClearBlock( Console.WindowTop, Console.WindowTop + 5 );
        Console.SetCursorPosition( 0, Console.WindowTop );
        WriteCurrentPath();
    }

-> Expected Behaviour

-> Calling fileScout from command line

In directories where the amount of files don't fill the window beyond the height, it is displayed properly. However, when the list gets bigger than the window height everything gets displayed incorrectly and the cursor behaves strangely.

This only occurs when calling the app via the command line. Not when double clicking on it or running it via the IDE (which is the same as double clicking).

Why would a console application, that's meant to be run in a terminal, behave differently from calling it from an actual terminal to running it from the file explorer?

The issue is the same for powershell. I also just realised it works correctly on ConEmu emulating cmd or powershell. So the issue only happens in vanilla powershell and command prompt.

EDIT: I also just realised the problem is that when I call Console.SetCursorPosition(...) it works in the IDE but not in command Line. I suspect whatever is causing the cursor to not change is the culprit.

  • Could be window/buffer width settings, maybe word wrap too. Check the layout tab (alt+space -> Properties -> Layout). Could you add screenshots about working and nonworking scenarios? – vonPryz May 06 '19 at 05:05
  • Since you're using ConEmu, check its integration settings. They could interfere with console sessions. – montonero May 06 '19 at 08:13

0 Answers0