0

I'm a really really noob at coding it's my first program and my first time I touch a coding software.

First let me show you what I have :

private void SaveClose_Click(object sender, RoutedEventArgs e)
{
    using (var fs = new FileStream(@"Resources\arguments.txt", FileMode.Truncate))
    {
    }

    if (Windowed.IsChecked == true)
        windowed = true;
    else
        windowed = false;

    string text = File.ReadAllText(@"Resources\arguments.txt");

    string createTextWindowed = "-screen-fullscreen 0" + Environment.NewLine;
    File.WriteAllText(@"Resources\arguments.txt", createTextWindowed);

    string createTextFullscreen = "-screen-fullscreen 1" + Environment.NewLine;
    File.WriteAllText(@"Resources\arguments.txt", createTextFullscreen);


    if (windowed == true)
        createTextWindowed 
    else
        createTextFullscreen
}

Lines 21 and 23 (createTextWindowed and createTextFullscreen) are errors and I want to fix them but I'm a noob so I don't know how to use the strings I've made, I want to make the program write in the txt file "-screen-fullscreen 0" if windowed = true (I think you got it) and 1 if it's false, I tried this but of course it doesn't work :')

Balagurunathan Marimuthu
  • 2,927
  • 4
  • 31
  • 44
Francefire
  • 81
  • 1
  • 9

5 Answers5

1

The logical order of your code needs to change, so:

    string text = File.ReadAllText(@"Resources\arguments.txt");

    if (Windowed.IsChecked)
    {
      text = text + "-screen-fullscreen 0" + Environment.NewLine;
    }
    else
    {
      text = text + "-screen-fullscreen 1" + Environment.NewLine;
    }

    File.WriteAllText(@"Resources\arguments.txt", text);

The using directive is not necessary so you can remove that.

Also if the windowed bool is not used outside of this scope, then its not necessary and you can just use the Windowed.IsChecked in the if statement directly. This neatens the code up a bit.

And then you need to think about the flow and logic of the code. So maybe map it out on a piece of paper, my understanding is this:

  1. Read text from "Resources\arguments.txt"
  2. If Windowed.IsChecked is true add argument -screen-fullscreen 0 to text
  3. Else if Windowed.IsChecked is false add argument -screen-fullscreen 0 to text
  4. Save the new text to file.

Maybe look back on your original code with this steps in mind, and see if you can spot the redundant code.

Mark Atkinson
  • 458
  • 8
  • 14
1
  1. You don't need this because File.WriteAllText truncates the file anyways.

    using (var fs = new FileStream(@"Resources\arguments.txt", FileMode.Truncate))
    {
    }
    
  2. Remove this as well since it doesn't seem that you are using it

    string text = File.ReadAllText(@"Resources\arguments.txt");
    
  3. Define a string variable in which to set the text you want to write depending on if it's windowed or not.

    string textWrite;
    
    if (windowed == true)
    {
        textWrite = "-screen-fullscreen 0" + Environment.NewLine;
    }
    else
    {
        textWrite = "-screen-fullscreen 1" + Environment.NewLine;
    }
    
  4. And lastly just write the text in the file

    File.WriteAllText(@"Resources\arguments.txt", textWrite);
    

Here is the full code of the function after changes:

private void SaveClose_Click(object sender, RoutedEventArgs e)
{
    if (Windowed.IsChecked == true)
        windowed = true;
    else
        windowed = false;

    string textWrite;

    if (windowed == true)
    {
        textWrite = "-screen-fullscreen 0" + Environment.NewLine;
    }
    else
    {
        textWrite = "-screen-fullscreen 1" + Environment.NewLine;
    }

    File.WriteAllText(@"Resources\arguments.txt", textWrite);
}
R.Rusev
  • 1,111
  • 15
  • 19
  • It seems they may be looking to append an argument to pre-existing text in the "Resources\arguments.txt" file? Hard to know for sure without confirmation. – Mark Atkinson Oct 13 '16 at 08:54
  • I assumed this was a mistaken copy and paste from another answer as an attempt to write to the file, not knowing what the Filemode.Truncate actually did. The body wasn't even in the using directive. However you do make a good point. We will need to wait and see. – Mark Atkinson Oct 13 '16 at 08:59
  • But now I have a problem, I have to put the full directory of the "argument.txt" (C:\Users\baske\Desktop\Launcher\WpfApplication2\WpfApplication2\Resources\arguments.txt) but in the future I want the program to be on a lot of different computer, and I know that not everybody has the same directory as me for the launcher (and the game) can you please help me with that ? When I put `@"Ressources\arguments.txt"` it doesn't work, it's not editing the txt file – Francefire Oct 13 '16 at 09:11
  • @Francefire Have a look at http://stackoverflow.com/questions/938421/getting-the-applications-directory-from-a-wpf-application – R.Rusev Oct 13 '16 at 09:14
  • I am really noob, I don't know where to place them, I tried `using System.AppDomain.CurrentDomain.BaseDirectory;` but "CurrentDomain" apears as an error :( @R.Rusev – Francefire Oct 13 '16 at 09:23
0
        string createText;

        if (Windowed.IsChecked){
        createText= "-screen-fullscreen 0" + Environment.NewLine;        
        }
        else{
        createText = "-screen-fullscreen 1" + Environment.NewLine;        
    }
File.WriteAllText(@"Resources\arguments.txt", createText);
mbadeveloper
  • 1,272
  • 9
  • 16
  • @Francefire is that what you want to do? I modify your code to be more simple – mbadeveloper Oct 13 '16 at 08:44
  • @mbadeveloper why you need `FileStream` you are using `File.WriteAllText` – Mostafiz Oct 13 '16 at 08:47
  • Mostafiz you are true I removed the file stream @Francefire please use the new code – mbadeveloper Oct 13 '16 at 08:49
  • I'm totally confused now, when I want to try it it says that my txt file is used by another procesus, but no... It shows me a line in another Window (the main windows) but this line is supposed to do something only when I click a button – Francefire Oct 13 '16 at 08:58
  • @Francefire I am not sure why you need to read the file in the first statement? if you want only to write the text to the file and lose the file contents because is not important for you then remove the first line string text = File.ReadAllText(@"Resources\arguments.txt"); – mbadeveloper Oct 13 '16 at 09:02
  • Yes you are right too, the abswer of R. Rusev was really noob friendly (like I am) but thanks a lot either man <3 – Francefire Oct 13 '16 at 09:06
  • I did but I signed up very recently so it doesn't work ^^ – Francefire Oct 13 '16 at 09:15
0

Try this one

private void SaveClose_Click(object sender, RoutedEventArgs e)
{
    string text = File.ReadAllText(@"Resources\arguments.txt");

    string createTextWindowed = "-screen-fullscreen 0" + Environment.NewLine;
    string createTextFullscreen = "-screen-fullscreen 1" + Environment.NewLine;

    if (Windowed.IsChecked == true)
    {
          File.WriteAllText(@"Resources\arguments.txt", createTextWindowed);
    }
    else
    {
          File.WriteAllText(@"Resources\arguments.txt", createTextFullscreen);
    }
}
Mostafiz
  • 7,243
  • 3
  • 28
  • 42
0

You just need to change your final logic a bit:

if (windowed == true)
{
    string createTextWindowed = "-screen-fullscreen 0" + Environment.NewLine;
    File.WriteAllText(@"Resources\arguments.txt", createTextWindowed);
}
else
{
    string createTextFullscreen = "-screen-fullscreen 1" + Environment.NewLine;
    File.WriteAllText(@"Resources\arguments.txt", createTextFullscreen);
}

Then drop the check at the end

LordWilmore
  • 2,829
  • 2
  • 25
  • 30