Answer to Question #42482 in C# for ankit

Question #42482
Sharon has started to use both the stock details management and the file information viewer
features of the application. However, while using the file information viewer application, she
accidentally enters the path of a folder that does not exist on her computer. This caused the
application to stop responding. In addition, the format in which the stock details are displayed is
not clear. Therefore, she asks Hayley to modify the application so that:
The file information viewer does not stop responding in case a specified folder does not exist.
The stock details management application adds a line break before every stock details entry
whenever a new entry is added to the stock details file.
Expert's answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace project
class Program
static void Main(string[] args)
int line = 2;
ConsoleKeyInfo key;
Console.WriteLine("Choose action to do(ESC - exit):\n");
Console.WriteLine("Show files from directory *");
Console.WriteLine("Write data to the stock file");
Console.SetCursorPosition(32, 2);
key = Console.ReadKey(true);
switch (key.Key)
case ConsoleKey.UpArrow:
Console.SetCursorPosition(32, line);
Console.Write(" ");
if (line == 2) line = 3;
else line--;
Console.SetCursorPosition(32, line);
case ConsoleKey.DownArrow:
Console.SetCursorPosition(32, line);
Console.Write(" ");
if (line == 3) line = 2;
else line++;
Console.SetCursorPosition(32, line);
case ConsoleKey.Escape: Environment.Exit(0); break;
} while (key.Key != ConsoleKey.Enter);
if (line == 2) showDir();
public static void showDir()
Console.WriteLine(@"Enter the pass to the directory:(Example -> D:\)");
string filePass = Console.ReadLine(); //folder pass
if (System.IO.Directory.Exists(filePass)) // Does folder exist?
string[] fileNames = System.IO.Directory.GetFiles(filePass); //getting names of files
Console.WriteLine("Directory: {0} contains {1} file(s)", filePass, fileNames.Length);
for (int i = 0; i < fileNames.Length; i++)
if (System.IO.File.Exists(fileNames[i]))
string name = "";
FileInfo fi = new FileInfo(fileNames[i]);
DateTime cr_date;
string date = "";
name = fileNames[i].Substring(filePass.Length);
cr_date = System.IO.File.GetCreationTime(fileNames[i]); //creation date
date = cr_date.Day + "." + cr_date.Month + "." + cr_date.Year;
Console.WriteLine("{0,30} \t{1,10} byte(s) \t {2}", name, fi.Length, date);
else Console.Write("Pass doesn`t exist");
public static void Write()
string fileName = "stockDetails.txt";
Console.WriteLine(@"Please enter the directory of the stockDetails.txt:");
string pass = Console.ReadLine(); //folder pass
if (!Directory.Exists(pass))
Console.WriteLine("Directory doesn`t exist!");
FileStream fs; //object to write data
if (!File.Exists(pass + fileName)) //File exist?
Console.Write(pass + fileName + " doesn`t exist. We will create it.\n");
fs = new FileStream(pass + fileName, FileMode.Create, FileAccess.Write); //Create file for writing
fs = new FileStream(pass + fileName, FileMode.Open, FileAccess.Write);
Console.WriteLine(fileName + " Successfully opened");// Just open it
string Data = "";
Console.WriteLine("Please enter some data to write into the file:");
Data = Console.ReadLine();//Getting any input information from console
Byte[] info = new UTF8Encoding(true).GetBytes(Data + "\r\n"); //Convert your data to Bytes
fs.Seek(0, SeekOrigin.End); //Seeking for the last point in the file
fs.Write(info, 0, info.Length); // Writing data
fs.Close();//Do not forget to close a file
Console.WriteLine("Successfully done");

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!


No comments. Be the first!

Leave a comment

Ask Your question

New on Blog