1

How would I go about saving a user form input from a view to an existing text file with the ASP.NET MVC framework?

1 Answer 1

1

So to start of, you would accept some code from the user and then save it:

string userInput =Console.Readline();

Then you would use the code WriteAllText(). This allows you to create a new file and writes the contents to it. If the file already exists, it will be overwritten. So basically:

string userInput =Console.Readline();
File.WriteAllText("thenameyouwanttogivetoyourfile.txt", userInput);

This reads the file and then outputs it

string readText = File.ReadAllText("thenameyouwanttogivetoyourfile.txt");  
Console.WriteLine(readText);

Thats how you create a new file. To overwrite a file that already exists, you do the same thing but with the keyword Create(). This creates or overwrites a file and if you want to replace the contents of one file with another, use Replace().

For futher help try this link: https://learn.microsoft.com/en-us/dotnet/api/system.io.file?view=netframework-4.8

Sign up to request clarification or add additional context in comments.

2 Comments

Could you accept the answer if it works for you. This way, it saves other people time and effort.
I need a bit more guidance about how this would work in the MVC framework

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.