• Najnowsze pytania
  • Bez odpowiedzi
  • Zadaj pytanie
  • Kategorie
  • Tagi
  • Zdobyte punkty
  • Ekipa ninja
  • IRC
  • FAQ
  • Regulamin
  • Książki warte uwagi

Błąd w pliku IL2CPU

Object Storage Arubacloud
0 głosów
81 wizyt
pytanie zadane 30 czerwca 2023 w C# przez Iskiereczka Użytkownik (880 p.)

Kilka dni temu zacząłem pisać system operacyjny w Cosmosie. Dzisiaj wyskoczył oto ten problem:
 

Ważność	Kod	Opis	Projekt	Plik	Wiersz	Stan pominięcia
Błąd		Exception: System.Exception:	SKSOS	C:\...\source\repos\SKSOS\IL2CPU	1	

Wyskoczył podczas debugu projektu. Tworzyłem też inne projekt w Cosmosie dzisiaj(te domyślne) aby sprawdzić czy to nie problem z moją instalacją Cosmosa, ale te domyślne projekty działały. Zmieniałem tylko kod w Kernel.cs

Kod z Kernel.cs:
 

using Cosmos.System.FileSystem.VFS;
using System;
using System.Collections.Generic;
using System.IO;
using Sys = Cosmos.System;

namespace SKSOS
{
    public class Kernel : Sys.Kernel
    {
        private static string currentDirectory;
        private static string usersDirectory = "/usersfiles";

        protected override void BeforeRun()
        {
            var fs = new Sys.FileSystem.CosmosVFS();
            Sys.FileSystem.VFS.VFSManager.RegisterVFS(fs);

            currentDirectory = usersDirectory;
            Console.WriteLine("SKSOS [Version 1.0]");
            Console.WriteLine("-------------------");
        }

        protected override void Run()
        {
            DOSManager();
        }

        public static void DOSManager()
        {
            Console.Write($"{currentDirectory}> ");
            var dosinput = Console.ReadLine();

            if (dosinput.StartsWith("print "))
            {
                string result = dosinput.Substring(6);
                Console.WriteLine(result);
            }
            else if (dosinput.StartsWith("read "))
            {
                string fileName = dosinput.Substring(5);
                string filePath = GetFilePath(fileName);

                try
                {
                    if (FileExists(filePath))
                    {
                        string fileContent = File.ReadAllText(filePath);
                        Console.WriteLine(fileContent);
                    }
                    else
                    {
                        Console.WriteLine("File not found: " + fileName);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error reading file: " + ex.Message);
                }
            }
            else if (dosinput.StartsWith("write "))
            {
                int index = dosinput.IndexOf(' ', 6);

                if (index != -1)
                {
                    string fileName = dosinput.Substring(6, index - 6);
                    string filePath = GetFilePath(fileName);
                    string content = dosinput.Substring(index + 1);

                    if (CanWriteToDirectory(usersDirectory))
                    {
                        try
                        {
                            File.WriteAllText(filePath, content);
                            Console.WriteLine("File written successfully.");
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("Error writing file: " + ex.Message);
                        }
                    }
                    else
                    {
                        Console.WriteLine("No write permissions for the directory: " + usersDirectory);
                    }
                }
            }
            else if (dosinput.StartsWith("del "))
            {
                string fileName = dosinput.Substring(4);
                string filePath = GetFilePath(fileName);

                try
                {
                    if (FileExists(filePath))
                    {
                        File.Delete(filePath);
                        Console.WriteLine("File deleted successfully.");
                    }
                    else
                    {
                        Console.WriteLine("File not found: " + fileName);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error deleting file: " + ex.Message);
                }
            }
            else if (dosinput == "cd ..")
            {
                // Przejdź do nadrzędnego katalogu
                int lastSlashIndex = currentDirectory.LastIndexOf('/');
                if (lastSlashIndex != -1)
                {
                    currentDirectory = currentDirectory.Substring(0, lastSlashIndex);
                }
            }
            else if (dosinput.StartsWith("cd "))
            {
                string directoryPath = dosinput.Substring(3);

                if (directoryPath == "..")
                {
                    // Przejdź do nadrzędnego katalogu
                    int lastSlashIndex = currentDirectory.LastIndexOf('/');
                    if (lastSlashIndex != -1)
                    {
                        currentDirectory = currentDirectory.Substring(0, lastSlashIndex);
                    }
                }
                else
                {
                    string newDirectory = Path.Combine(currentDirectory, directoryPath);

                    if (DirectoryExists(newDirectory))
                    {
                        currentDirectory = newDirectory;
                    }
                    else
                    {
                        Console.WriteLine("Directory not found: " + directoryPath);
                    }
                }
            }
            else if (dosinput == "clear")
            {
                Console.Clear();
                Console.WriteLine("SKSOS [Version 1.0]");
                Console.WriteLine("-------------------");
            }
            else if (dosinput == "hi" || dosinput == "Hi")
            {
                Console.WriteLine("Hi!");
            }
        }

        private static bool FileExists(string filePath)
        {
            return File.Exists(filePath);
        }

        private static bool DirectoryExists(string directoryPath)
        {
            return Directory.Exists(directoryPath);
        }

        private static bool CanWriteToDirectory(string directoryPath)
        {
            try
            {
                // Sprawdź atrybuty katalogu
                var directoryAttributes = File.GetAttributes(directoryPath);
                var isReadOnly = (directoryAttributes & FileAttributes.ReadOnly) == FileAttributes.ReadOnly;
                return !isReadOnly;
            }
            catch
            {
                return false;
            }
        }

        private static string GetFilePath(string fileName)
        {
            return Path.Combine(currentDirectory, fileName);
        }
    }
}

komentarz 30 czerwca 2023 przez VBService Ekspert (254,320 p.)

Exception: System.Exception: 

jest zbyt ogólnym opisem błędu, a IMO

C:\...\source\repos\SKSOS\IL2CPU

wskazuje, że problematyczny plik znajduje się w folderze IL2CPU.

 

Zmieniałem tylko kod w Kernel.cs

możesz, proszę, też pokazać kod z pliku Kernel.cs przed zmianami.

komentarz 30 czerwca 2023 przez VBService Ekspert (254,320 p.)
edycja 30 czerwca 2023 przez VBService

BTW,

spróbuj użyć Environment

private static string usersDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
private static string currentDirectory = usersDirectory; 

sprawdź:

 

proponuje zamiast "długich", "nieczytelnych" if-ów

if (dosinput.StartsWith("print "))
{
    string result = dosinput.Substring(6);
    Console.WriteLine(result);
}
else if (dosinput.StartsWith("read "))
{
    string fileName = dosinput.Substring(5);

    // ...
}

i "ręcznego" ustawiania dosinput.Substring(6)dosinput.Substring(5) itd.

użyć Split i Switch np.:

Console.Write($"{currentDirectory}> ");

string dosinput = Console.ReadLine().Trim();
string[] commandLine = dosinput.Split(' ');

string command = Shift(ref commandLine);
switch (command)
{
    case "print": Print(commandLine); break;
    case "clear": Clear(); break;

    // itd. ...

    default:
        Console.WriteLine($"Nie znane polecenie: {command}");
        break;
}

 

przykład

using System;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        //private static string usersDirectory = "/usersfiles";
        private static string usersDirectory = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
        private static string currentDirectory = usersDirectory;        

        static void Main(string[] args)
        {
            WelcomeMessage();
            Boolean mainLoop = true;

            while (mainLoop)
            {
                Console.Write($"{currentDirectory}> ");

                string dosinput = Console.ReadLine().Trim();
                string[] commandLine = dosinput.Split(' ');

                string command = Shift(ref commandLine);
                switch (command)
                {
                    case "PRINT":
                    case "Print":
                    case "print": Print(commandLine); break;

                    case "READ":
                    case "Read":
                    case "read": Read(commandLine); break;

                    case "WRITE":
                    case "Write":
                    case "write": Write(commandLine); break;                        

                    // itd. ...

                    case "CLEAR":
                    case "Clear":
                    case "clear": Clear(); break;

                    case "EXIT":
                    case "Exit":
                    case "exit": mainLoop = false; break; // ... wyjście z pętli while ... koniec programu

                    case "": continue; // String.Empty
                    default:
                        Console.WriteLine($"Nieznane polecenie: {command}");
                        break;
                }
            }

            // ... koniec programu
            //Console.ReadKey();
        }

        // ********* command ****************************

        private static void Print(string[] commandLine)
        {
            string result = string.Join(" ", commandLine);
            Console.WriteLine(result);
        }

        private static void Read(string[] commandLine)
        {
            string fileName = string.Join(" ", commandLine);

            Console.WriteLine(fileName); // dla demonstarcji

            //string filePath = GetFilePath(fileName);

            // ... reszta kodu
        }

        private static void Write(string[] commandLine)
        {
            string fileName = Shift(ref commandLine);
            string content = string.Join(" ", commandLine);

            Console.WriteLine($"Nazwa pliku: {fileName}"); // dla demonstarcji
            Console.WriteLine($"Tresc: {content}");

            //string filePath = GetFilePath(fileName);

            // ... reszta kodu
        }

        private static void Clear()
        {
            Console.Clear();
            //WelcomeMessage();
        }

        // ********* /command ***************************

        private static void WelcomeMessage()
        {            
            string message = "SKSOS [Version 1.0]  (©)2023 @Iskiereczka";

            Console.Clear();
            Console.WriteLine(message);
            Console.WriteLine(new string('-', message.Length));
        }

        private static string Shift(ref string[] commandLine)
        {
            if (commandLine.Length > 0)
            {
                string firstElement = commandLine[0]; // commandLine.First()
                commandLine = commandLine.Skip(1).ToArray();
                return firstElement;
            }
            else
            {
                return String.Empty;
            }
        }
    }
}

 

inny wariant dla switch()

                switch (command.ToLower())
                {
                    case "print": Print(commandLine); break;
                    case  "read": Read(commandLine); break;

                    // itd.
                }

i dla dosinput.Substring(6)dosinput.Substring(5) itd.

if (dosinput.StartsWith("print "))
{
    string result = dosinput.Substring(dosinput.IndexOf(' ') + 1).Trim();
    Console.WriteLine(result);
}
else if (dosinput.StartsWith("read "))
{
    string fileName = dosinput.Substring(dosinput.IndexOf(' ') + 1).Trim();

    // ...
}


 

Zaloguj lub zarejestruj się, aby odpowiedzieć na to pytanie.

Podobne pytania

+1 głos
1 odpowiedź 382 wizyt
pytanie zadane 11 maja 2021 w C# przez Iskiereczka Użytkownik (880 p.)
0 głosów
0 odpowiedzi 148 wizyt
pytanie zadane 14 listopada 2017 w C# przez mo290103 Obywatel (1,860 p.)
0 głosów
2 odpowiedzi 910 wizyt

92,620 zapytań

141,474 odpowiedzi

319,813 komentarzy

62,004 pasjonatów

Motyw:

Akcja Pajacyk

Pajacyk od wielu lat dożywia dzieci. Pomóż klikając w zielony brzuszek na stronie. Dziękujemy! ♡

Oto polecana książka warta uwagi.
Pełną listę książek znajdziesz tutaj.

Akademia Sekuraka

Kolejna edycja największej imprezy hakerskiej w Polsce, czyli Mega Sekurak Hacking Party odbędzie się już 20 maja 2024r. Z tej okazji mamy dla Was kod: pasjamshp - jeżeli wpiszecie go w koszyku, to wówczas otrzymacie 40% zniżki na bilet w wersji standard!

Więcej informacji na temat imprezy znajdziecie tutaj. Dziękujemy ekipie Sekuraka za taką fajną zniżkę dla wszystkich Pasjonatów!

Akademia Sekuraka

Niedawno wystartował dodruk tej świetnej, rozchwytywanej książki (około 940 stron). Mamy dla Was kod: pasja (wpiszcie go w koszyku), dzięki któremu otrzymujemy 10% zniżki - dziękujemy zaprzyjaźnionej ekipie Sekuraka za taki bonus dla Pasjonatów! Książka to pierwszy tom z serii o ITsec, który łagodnie wprowadzi w świat bezpieczeństwa IT każdą osobę - warto, polecamy!

...