C # Network application programming basic exercises and Answers (vi)

Source: Internet
Author: User
Tags copy
Programming | network


  1. Briefly answer the differences and links between the document and the stream.



Troubleshooting



The file and stream (stream) are different and connected. A file is an ordered  that is permanently stored on various media (removable disks, hard disks, CDs, and so on). It is a basic object for data read and write operations. Typically, files are organized in a tree directory, each with a file name, path to the file, creation time, access rights, and so on.



A stream is an abstract concept of a sequence of bytes, such as a file, a input, an internal process communication pipeline, or a TCP/IP socket that can be viewed as a stream. The stream provides a way to write bytes to the backing store and read bytes from the backing store.



  2. What directory management functions do the directory classes provide for us, and how are they implemented?



Troubleshooting



The directory class provides us with the ability to manage disks and directories, such as copying, moving, renaming, creating, and deleting directories, and getting and setting time information related to the creation, access, and write operations of the directory.



For example, the CreateDirectory method is used to create all directories in the specified path, and the Delete method deletes the specified directory; The Move method enables renaming or moving the directory; The Exists method is used to determine whether a given path references an existing directory on disk; The GetCurrentDirectory method is used to get the current working directory of the Application; The GetFiles method returns the name of the file in the specified directory, and so on.



  3. The main method of programming a comprehensive application directory class. First determines whether the specified directory exists, deletes the directory if it exists, and creates the directory if it does not exist. Then, move the directory, create a file in it, and count the files.



Troubleshooting



The list of procedures is as follows:



Using System;
Using System.IO;
Class Test
{
public static void Main ()
{
string path = @ "C:\MyDir";
String target = @ "C:\TestDir";
Try
{
if (! Directory.Exists (PATH))
{
Directory.CreateDirectory (path);
}
if (directory.exists (target))
{
Directory.delete (target, true);
}
Directory.move (path, target);
File.createtext (target + @ "\myfile.txt");
Console.WriteLine ("The number of files in {0} is {1}",
Target, Directory.GetFiles (target). Length);
}
catch (Exception e)
{
Console.WriteLine ("Operation failed: {0}", e.tostring ());
}
finally {}
}
}






4. Write programs that copy files to a specified path and allow overwriting of target files of the same name.



Troubleshooting



The list of procedures is as follows:



Using System;
Using System.IO;
Class Test
{
public static void Main ()
{
string path = @ "C:\temp\MyTest.txt";
String path2 = path + "Temp";
Try
{
using (FileStream fs = file.create (path)) {}
File.delete (path2);
File.Copy (path, path2);
Console.WriteLine ("{0} Copy to: {1}", path, path2);
File.Copy (Path, path2, true);
Console.WriteLine ("Second copy succeeds");
}
Catch
{
Console.WriteLine ("Duplicate copy not allowed");
}
}
}






5. Write programs that use the file class implementation to delete all files in the current directory.



Troubleshooting



The list of procedures is as follows:



using system;
using system.io;
Class filetest
{
Public static void main ()
{
Console.WriteLine ("Confirm deletion of the current directory All documents under it? ");
Console.WriteLine ("Click ' Y ' key to continue, other key cancel operation");
Int a = console.read ();
if (a ==  ' Y '  | |  a ==  ' y ') {
Console.WriteLine ("Deleting Files ...");
}
Else
{
Console.WriteLine ("User cancel operation");
return;
}
directoryinfo dir = new directoryinfo  (".");
foreach  (Fileinfo f in dir. GetFiles ())
{
F.delete ();
}
}
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.