C
C#2y ago
jaydactvyt

❔ 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
Florian Voß
Florian Voß2y ago
What’s the issue? if you don't get any error or exception then it should work. Describe the issue pls
jaydactvyt
jaydactvyt2y ago
my issue is that it wont write my commandEntered string to the text file at it's specified location.
Florian Voß
Florian Voß2y ago
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
jaydactvyt
jaydactvyt2y ago
I expect to go the Commands folder on my desktop the a file named Commands.txt
Florian Voß
Florian Voß2y ago
not location within the file missunderstood you
jaydactvyt
jaydactvyt2y ago
Basically everything works normally but I see no output to the Commands text file
Florian Voß
Florian Voß2y ago
would you like the Streamwriter to overwrite the file or to append the file? try specifying it with the second argument of StreamWriter
jaydactvyt
jaydactvyt2y ago
I want it to append to the file But I can't even get the text to show up
Florian Voß
Florian Voß2y ago
are you sure that the commandEntered variable contains the value you expect it to have? Have you debugged with a breakpoint?
jaydactvyt
jaydactvyt2y ago
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.
Florian Voß
Florian Voß2y ago
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 runtime
jaydactvyt
jaydactvyt2y ago
else 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(); }
Florian Voß
Florian Voß2y ago
maybe it doesn't hold what you expect
jaydactvyt
jaydactvyt2y ago
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.
Florian Voß
Florian Voß2y ago
can you show me the value of commandEntered at runtime pls and this
jaydactvyt
jaydactvyt2y ago
I forgot how to do that ,but I can send everything in the method and the methods it interacts with
Florian Voß
Florian Voß2y ago
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
jaydactvyt
jaydactvyt2y ago
k
Florian Voß
Florian Voß2y ago
another simple way would have been to just Console.WriteLine it
jaydactvyt
jaydactvyt2y ago
I'm using a windows form I still couldn't find the value of commandEntered
Florian Voß
Florian Voß2y ago
that shouldn't matter keep trying its important to be able to debug your code read or watch a guide if necessary
jaydactvyt
jaydactvyt2y ago
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
Florian Voß
Florian Voß2y ago
you think thats what commandEntered holds but you don't know thats why we debug
jaydactvyt
jaydactvyt2y ago
default: 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
Florian Voß
Florian Voß2y ago
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 that
jaydactvyt
jaydactvyt2y ago
I'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
Florian Voß
Florian Voß2y ago
do you use visual studio or visual studio code? i will send you a guide for debugging
jaydactvyt
jaydactvyt2y ago
Just regular visual studio
jaydactvyt
jaydactvyt2y ago
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
jaydactvyt
jaydactvyt2y ago
jaydactvyt
jaydactvyt2y ago
Pobiega
Pobiega2y ago
The path isn't a verbatim string literal So the backslashes are escaping
jaydactvyt
jaydactvyt2y ago
Does that mean I put the path in wrong
MODiX
MODiX2y ago
Pobiega#2671
REPL Result: Failure
Console.WriteLine("C:\Users\Sunwa\Desktop\Commands\Commands.txt");
Console.WriteLine(@"C:\Users\Sunwa\Desktop\Commands\Commands.txt");
Console.WriteLine("C:\Users\Sunwa\Desktop\Commands\Commands.txt");
Console.WriteLine(@"C:\Users\Sunwa\Desktop\Commands\Commands.txt");
Exception: CompilationErrorException
- Unrecognized escape sequence
- Unrecognized escape sequence
- Unrecognized escape sequence
- Unrecognized escape sequence
- Unrecognized escape sequence
- Unrecognized escape sequence
- Unrecognized escape sequence
- Unrecognized escape sequence
- Unrecognized escape sequence
- Unrecognized escape sequence
Compile: 567.837ms | Execution: 0.000ms | React with ❌ to remove this embed.
Pobiega
Pobiega2y ago
its even throwing errors because the first one isnt valid
MODiX
MODiX2y ago
Pobiega#2671
REPL Result: Success
Console.WriteLine(@"C:\Users\Sunwa\Desktop\Commands\Commands.txt");
Console.WriteLine(@"C:\Users\Sunwa\Desktop\Commands\Commands.txt");
Console Output
C:\Users\Sunwa\Desktop\Commands\Commands.txt
C:\Users\Sunwa\Desktop\Commands\Commands.txt
Compile: 487.586ms | Execution: 26.553ms | React with ❌ to remove this embed.
Pobiega
Pobiega2y ago
this one is fine however so yes, when entering text with significant backslashes, you need to be careful use verbatim strings (prefixed with @)
jaydactvyt
jaydactvyt2y ago
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
Pobiega
Pobiega2y ago
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:
File.WriteAllText("test.txt", "hello world");
File.WriteAllText("test.txt", "hello world");
run that program, open up the bin folder for that program and look for a "test.txt" file
Pobiega
Pobiega2y ago
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.
jaydactvyt
jaydactvyt2y ago
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
Accord
Accord2y ago
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.