❔ I Need help getting the Streamwriter object to write to a text file.
TextWriter sw = new StreamWriter ("C:\Users\Sunwa\Desktop\Commands\Commands.txt");
sw.Write(commandEntered);
sw.Flush();
sw.Close();
43 Replies
What’s the issue?
if you don't get any error or exception then it should work. Describe the issue pls
my issue is that it wont write my commandEntered string to the text file at it's specified location.
what location would you expect it at? I don't see a location specified
does it write the content into the file at all?
oh you mean file location
I expect to go the Commands folder on my desktop the a file named Commands.txt
not location within the file
missunderstood you
Basically everything works normally but I see no output to the Commands text file
would you like the Streamwriter to overwrite the file or to append the file? try specifying it with the second argument of StreamWriter
I want it to append to the file
But I can't even get the text to show up
are you sure that the
commandEntered
variable contains the value you expect it to have? Have you debugged with a breakpoint?I didn't set a breakpoint but as far as I'm aware the commandEnterd takes in a string from another string that its value passed in from a method.
so pass
true
as the second arg then to the StreamWriter constructor
place a break point and press debug to inspect the value of commandEntered
during runtimeelse if (BuiltInCommand == true && sAnswer.Contains("/response "))
{
sAnswer = sQuestion.Substring(10);
commandEntered = sAnswer;
SpeechToText(commandEntered);
TextWriter sw = new StreamWriter("C:\Users\Sunwa\Desktop\Commands\Commands.txt");
sw.Write(commandEntered);
sw.Flush();
sw.Close();
}
maybe it doesn't hold what you expect
When I start debugging and type in the /command or response everything execute accordingly. It just doesn't write the text in the commandEntered string to the text file.
can you show me the value of
commandEntered
at runtime pls
and thisI forgot how to do that ,but I can send everything in the method and the methods it interacts with
this is how
you place a breakpoint in the line where you want your program to stop
then you "run and debug" your code and it will stop on that line
then you can inspect the value of any variable either by hovering or other ways
k
another simple way would have been to just
Console.WriteLine
itI'm using a windows form
I still couldn't find the value of commandEntered
that shouldn't matter
keep trying
its important to be able to debug your code
read or watch a guide if necessary
I actually forgot that my commandEntered value is displayed in the windows forms app
User: /response my favorite color is blue
AI Assistant: Your wish is now my command.
The part after AI Assitant is the commandEntered value
you think thats what
commandEntered
holds but you don't know
thats why we debugdefault:
if(sQuestion.Contains("/command "))
{
textToSpeak = "The response syntax is as follows: /response[one-space][response text goes here]." +
"What would you like my response to be?";
sAnswer = textToSpeak;
txtAnswer.AppendText("AI Assistant: " + sAnswer.Replace("\n", "\r\n"));
return sAnswer; } else if (sQuestion.Contains("/response ")) { textToSpeak = "Your wish is now my command."; sAnswer = textToSpeak; txtAnswer.AppendText("AI Assistant: " + sAnswer.Replace("\n", "\r\n")); return sAnswer; } else { return sAnswer; } the sAnswer variable is what transfers back to the form class and give commandEntered it's value
return sAnswer; } else if (sQuestion.Contains("/response ")) { textToSpeak = "Your wish is now my command."; sAnswer = textToSpeak; txtAnswer.AppendText("AI Assistant: " + sAnswer.Replace("\n", "\r\n")); return sAnswer; } else { return sAnswer; } the sAnswer variable is what transfers back to the form class and give commandEntered it's value
bruh
debug your code and verify that
commandEntered
holds what you expect it to hold.
the answer will stay the same until you've done thatI'm trying but the watch windows is on the wrong file
lol
For some reason it's not letting me add a watch window to my form code
do you use visual studio or visual studio code?
i will send you a guide for debugging
Just regular visual studio
I've been following along and noticed my program doesn't stop at the breakpoint and maybe that whythe tool tips don't show up
I'm 99.5% sure commandEntered is the output of AI assistant because it gets it value from the sAnswer string that is returned
And my application speaks the commandEntered output in a voice
The path isn't a verbatim string literal
So the backslashes are escaping
Does that mean I put the path in wrong
Pobiega#2671
REPL Result: Failure
Exception: CompilationErrorException
Compile: 567.837ms | Execution: 0.000ms | React with ❌ to remove this embed.
its even throwing errors because the first one isnt valid
Pobiega#2671
REPL Result: Success
Console Output
Compile: 487.586ms | Execution: 26.553ms | React with ❌ to remove this embed.
this one is fine however
so yes, when entering text with significant backslashes, you need to be careful
use verbatim strings (prefixed with
@
)I just tried the new path and it's still not writing to my text file. Even my admin account isn't working
maybe visual studio 2019 is a dud
Its much more likely that your code isn't correct.
So, start a new console app from scratch and have it contain only one line:
run that program, open up the bin folder for that program and look for a "test.txt" file
if that works, try with the other path. Be aware that hardcoding a path including a username is usually a bad idea, and you should instead build the path using https://learn.microsoft.com/en-us/dotnet/api/system.environment.specialfolder?view=net-7.0
Environment.SpecialFolder Enum (System)
Specifies enumerated constants used to retrieve directory paths to system special folders.
Your code worked in a console app but it didn't work in my windows forms app.
However my modified version could be the culprit
System.IO.File.WriteAllText(@"C:\Users\Sunwa\Desktop\Commands\Commands.txt", commandEntered.ToString() + "\n");
that modified code worked in the console app but the windows forms app said no
I'm so confused
I found the issue
it was in my logic
the code should work once I fix it
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.