66 982
Assignments Done
99,2%
Successfully Done
In November 2018

Answer to Question #41745 in Other Programming & Computer Science for chandan

Question #41745
As part of her business activities, Sharon has created a large number of files and folders. Whenever she needs to view the details of the files stored in a folder, Sharon browses to the folder using Windows Explorer. However, she finds it cumbersome to browse to each individual folder to view the desired details. Therefore, she asks Hayley to modify the application to accept the name of a folder and display the following details of the files located in the folder: File name File size File creation date
Write the code that Hayley should write to create the desired file information viewer application.
Expert's answer
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
namespace Task
{
class Program
{
  static void Main(string[] args)
  {
 showDir();
Console.ReadKey();
  
  }
  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!");
return;
}
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 
}
else
{
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!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions