C # file read/write

Source: Internet
Author: User

Static File class:

Methods included:

Exists (filename)-check whether the specified file exists.

Copy ("thesource file", "the new file") -- copy the file from the original location to the target location

Create () -- create a file from a specified path

Delete () -- delete an object

Open () -- returns the filestream object from the specified path

Move () -- move the specified file to a new location. You can specify different names for the file in the new location!

Fileinfo is a non-static class. No static method!

Obtain a Logical Disk:

String [] driver = directory. getlogicaldrives ();

Get ApplicationProgramCurrent working directory

String Path = directory. getcurrentdirectory ();

Get the name of the subdirectory:

String filename = path. getfilename (@ "D: \ TXT \ txt.txt"); // return txt.txt

Get the list of file names in the directory

String [] getf = directory. getfiles (@ "D: \ TXT \");

Code
Using System;
Using System. Collections. Generic;
Using System. text;
Using System. IO;

Namespace Leleapplication1
{
Class Program
{
Static   Void Main ( String [] ARGs)
{
Fileinfotest ();
Filetest ();
Dirt ();
Console. readkey ();
}
Static   Void Fileinfotest ()
{
Fileinfo fl =   New Fileinfo ( @" D: \ TXT \ txt.txt " );
If (FL. exists)
{
Console. writeline ( " The file exist! " );

}
Else
{
Console. writeline ( " The fiel is not exist! " );
}
}
Static   Void Filetest ()
{
If (File. exists ( @" D: \ TXT \ txt.txt " ))
{
Console. writeline ( " The file is exist, it was checked by the static class file! " );
// File. Copy (@ "D: \ TXT \ txt.txt", @ "D: \ TXT \ XX \ t.txt ");
}
Else
{
Console. writeline ( " The file is not exist! " );
}
}
Static   Void Dirt ()
{
String [] Driver = Directory. getlogicaldrives ();
String Path = Directory. getcurrentdirectory ();
Console. writeline (PATH );
String Filename = Path. getfilename ( @" D: \ TXT \ txt.txt " );
String [] Getf = Directory. getfiles ( @" D: \ TXT \ " );
Foreach ( String Name In Getf)
{
Console. writeline (name );
}
Console. writeline ( " The file name is {0} " , Filename );
Foreach ( String ARG In Driver)
{
Console. writeline (ARG );
}
}
Static   Void Createdirt ()
{
Directoryinfo dirin =   New Directoryinfo ();

}
}
}

 

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.