Search code examples
c#filexnastreamreaderstreamwriter

How to open a textfile in XNA/monogame(beginner here)?


Okay, so i am working in xna and i want to open this textfile which should open in a textfile. Here is the code:

 if (Keyboard.GetState().IsKeyDown(Keys.G) == true) 
                {
                    var fileToOpen = "Name.txt";
                    var process = new Process();
                    process.StartInfo = new ProcessStartInfo()
                    {
                        UseShellExecute = true,
                        FileName = fileToOpen
                    };

                    process.Start();
                    process.WaitForExit();
                }

However an error occurs and cant find the textfile to open. I did this in a normal consol application and just added a new item textfile to the project and it worked fine in the console application, however in XNA it does not seem to work at all.

Also im really not well educated in file directory things and need a quick fix. The text files are placed in this area:
file location image

I hope this is of somehelp im trying to give as much information as possible. Just to note streamwriting to textfiles in the directory location shown in the image link works perfectly fine and i just give the name of the file as shown below:

if (player.GetRec.Intersects(map.sharkRec))
                {
                    using (StreamWriter writer = new StreamWriter("CurrentScore.txt"))
                    {
                        writer.Write(time);

                    }
                    player.Position = new Vector2(64,100);


                    mCurrentScreen = ScreenState.leaderboard;
                }

However it just didt seem to work when i want to open the textfile in notepad and allow for typing to be done in the textfile in notepad. The reason why i want to open a text file for typing is the user entering there name and i dont have knowledge or the time to do XNA textbox input creation which seems complicated from the tutorials i have seen, which is why i want to open the textfile in notepad for editing. Furthermore this is going to be used on other people's computers so if directorys have to be used i need a directory that will work on other computers as well as my own, just to note directory entering seems to confuse me.

Hope i have given enough information and i really hope someone can help this beginner out here :)


Solution

  • For this to work, your code should be something like this:

    
    
        using(StreamWriter ...)
        {
            show textbox so the user can see what he's typing
            for each keypress add the letter
            exit on ESC button (for example)
            delete char on Backspace
            etc...
        }
    
    

    Now, I personaly don't recommend this type of code. Open the file, do what you have to do with it and close it. The code below is how I programmed textbox for my game. I hope it helps (you could say this is more a little tutoial for better approach to the problem instead an answer to the exact problem you put up for yourself).

    
    
        namespace Acircalipse.MenuClasses
        {
            public class TextBox:MenuItem
            {
                private string originTitle;
            public string Text
            {
            get
            {
            return title.Replace(originTitle, "").Replace(Menu.separator, "");
            }
            }
            public int index, max;
        
            public      TextBox     (string Title, string text = "", int MaxCharacters = 14)
            {
            originTitle = Title;
            index = 0;
            max = MaxCharacters;
            SetText(text);
            }
            public void     SetText     (string text)
            {
            if (text.Length > max) text = text.Substring(0, max);
            title = originTitle + Menu.separator + text;
            }
            
            public void     ChangeIndex     (int howMuch)
            {
            index += howMuch;   
            ChangeCar(index);
            }
            public void     AddChar     (int index = 0)
            {
            SetText(Text + Menu.Wheel(index));
            }
            public void     ChangeCar       (int index)
            {
            if(Text.Length > 0)
            SetText(Text.Substring(0, Text.Length - 1) + Menu.Wheel(index));
            }
            public void     DeleteChar      ()
            {
            if (Text.Length > 0)
            SetText(Text.Substring(0, Text.Length - 1));
            }
            }
        }
    

    Where the Menu.Wheel(int index) is simply an array of available character for user to type in

    
    
        public static char Wheel(int index)
        {
            string charWheel = " ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
            int max = charWheel.Length;
            while (index > max) index -= max;
            while (index ("less than" sign here) 0) index += max;
            return charWheel[index];
        }
    
    

    Where Menu.separator is a string ": ".

    The code is pretty self explanatory.
    Now, when using this class, you'll have to have one bool to see if the user has activated this textbox, and if he has, add text on keypress. If the textbox is inactive, just continue your normal update

    What you have to understand is that textbox is a simple string witch is updated when textbox is active, and just showed when not active.
    Using this simple and on point definition of TextBox, you can simply create your own class that will work the way you want to.