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 :')

5 Answers 5

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);
}
Sign up to request clarification or add additional context in comments.

5 Comments

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.
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.
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 Have a look at stackoverflow.com/questions/938421/…
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
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.

Comments

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

Comments

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);
    }
}

Comments

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);

7 Comments

@Francefire is that what you want to do? I modify your code to be more simple
@mbadeveloper why you need FileStream you are using File.WriteAllText
Mostafiz you are true I removed the file stream @Francefire please use the new code
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 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");
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.