Answer to Question #41742 in C# for chandan

Question #41742
Hayley has started the application development. She needs to ensure that the application meets the following requirements:
When the application starts, it should display a message asking the user to enter either option d or v. The application should execute the following actions for the two options: d: The application should display the file extension of the stock details. v: The application should open the stock details file in the read-only mode and display its contents.
Write the code snippet that Hayley should use to implement the desired functionality in the application.
In case the user presses any other key, the application should open the stock details file in the read/write mode, display a message to enter new stock details, and save the details that the user has entered. Write the code snippet that Hayley should use to accomplish the preceding task.
1
Expert's answer
2014-05-05T14:29:37-0400
using System;
using System.IO;
namespace d_v_app
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Please press appropriate key:\n");
Console.WriteLine("d: Show the file extension of the stock details file");
Console.WriteLine("v: Display the content of stock details file ");

FileStream fs;
switch (Console.ReadKey().Key)
{
case ConsoleKey.V:
Console.Clear();
fs = new FileStream(Directory.GetCurrentDirectory() + @"\StockDetails.txt", FileMode.OpenOrCreate, FileAccess.Read);
Console.WriteLine("StockDetails.txt contains next information:");
Console.WriteLine("_________________________________________________________");
for (int i = 0; i < fs.Length; i++)
{
Console.Write((char)fs.ReadByte());
}
Console.WriteLine("\n_________________________________________________________");
fs.Close();
break;
case ConsoleKey.D:
Console.Clear();
Console.Write("StockDetails file has the next extension: .txt");

break;
default:
Console.Clear();
File.Delete(Directory.GetCurrentDirectory() + @"\StockDetails.txt");
fs = new FileStream(Directory.GetCurrentDirectory() + @"\StockDetails.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite);

Console.WriteLine("Please enter some data to the StockDetails.txt :");
string m = Console.ReadLine();
Byte[] bt = new byte[ m.Length];
for(int i=0;i<m.Length;i++) bt[i]=(byte)m[i];
fs.Write(bt, 0, (int)bt.Length);
fs.Close();
break;
}
Console.WriteLine("\nPress any key to exit...");
Console.Read();
}
}
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS