Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Description: Save a text document to disk based on a name and content provided by the user. Purpose: This application provides experience with user input

Description: Save a text document to disk based on a name and content provided by the user.

Purpose: This application provides experience with user input and interaction in the Console, writing files to disk, working with exceptions, and writing programs in C#/.NET.

Requirements:

Project Name: Document Target Platform: Console Programming Language: C#

Documentation:

  • Types and variables (Links to an external site.) (Microsoft)
  • Console.ReadLine Method () (Links to an external site.) (Microsoft)
  • Strings (C# Programming Guide) (Links to an external site.) (Microsoft)
  • How to read from and write to a text file by using Visual C# (Links to an external site.) (Microsoft)
  • StreamWriter Class (Links to an external site.) (Microsoft)
  • Exceptions and Exception Handling (C# Programming Guide) (Links to an external site.) (Microsoft)

This program is to display the name of the app Document followed by a blank line, prompt the user for the name and content for a document, save the document to the current directory, and display a message if it was successful or unsuccessful based on a specification provided. The filename of the document is the name provided by the user with .txt appended to it.

When the program runs:

  1. Display Document followed by a blank line.
  2. Prompt the user for the name of the document.
  3. Prompt the user for the content that is to be in the document.
  4. Append .txt to the name and use it as the file name.
  5. Save the content to a file in the current directory.
  6. If an exception occurs, output the exception message and exit.
  7. If an exception does not occur, output [filename] was successfully saved. The document contains [count] characters. and exit. [filename] and [count] are placeholders for the filename of the document and the number of characters it contains.

Note that the requirements say the file is to be saved in the current directory. This means you can use just the file name as the path for the file. If you do that, the file will be saved in the current directory for the application. When I created the project and called it Document, a directory called Document was created that contains Document.sln and another directory called Document. The Document directory inside of the project is the current directory when the application runs and is where you will find the files that are saved.

Optional Requirements:

The following requirements are not required, but you should try to implement them if you can.

If the name provided by the user already ends in .txt, do not append .txt to the name to create the filename. If it already has .txt on the end, use it as-is.

Close the stream that is opened in the finally block of a try-catch-finally. Caution: test the variable holding the reference to the stream to make sure it is not null before calling the Close method on it. Calling a method on a null reference will crash the application.

After the document is saved or fails to save, prompt the user if they want to save another document. If they do, prompt them again for input. If not, exit the program.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Go, do not wait until I come

Answered: 1 week ago

Question

Design a health and safety policy.

Answered: 1 week ago