Application of C # directory and directory info and file reading and writing

Source: Internet
Author: User
Tags foreach file size file upload tostring

The application of directory and directory info

In the actual application of asp,net, upload files to the implementation process to use the operation of the directory, to the actual file upload work. The following analysis is used in C # to complete the file upload process of the core code, where the control fileupload used to get files/

-------------Code Snippet----------

string filename = fileuploadfilename;//Gets the name of the uploaded file

String filetype = fileuploadfilename.substring (filename. LastIndexOf (".") +1). ToLower ();

Format of uploaded files

if (filetype = = "rar" | | filetype = = "Doc" | | filetype = "TXT" | | filetype = = "pdf")

{//Judge whether the file format is the specified format

if (fileuploadfilebytes.length <= 50 * 1024 * 1024)//Determine whether the file size meets the requirements

{

DirectoryInfo filedir = new DirectoryInfo (Server.MapPath ("fileupload/"));//Get folder FileUpload relative directory in the system.

Create a DirectoryInfo instance object Filedir to point to the directory where the uploaded file is saved.

if (! Directory. Exists (Filedir. ToString ()) A static method of the Directory class that is used to determine whether a directory exists

{

Directory.CreateDirectory (Filedir. ToString ());//If the requested directory does not exist, call directory static method CreateDirectory to create the directory.

}

-------------Code Snippet----------

DirectoryInfo Common methods:

directoryinfo dif = new directoryi NFO ("C:\\ABCD");

Directoryinfo temp = new directoryinfo ("C:\\atest");

Directoryinfo temp3 = new directoryinfo ("c:\\");

if  (!dif. Exists)

dif. Create ();

if  (!temp. Exists)

Directory.CreateDirectory (temp. ToString ());

if  (temp. Exists)

Temp. Delete ();

Dif. MoveTo (temp. ToString ());

Directoryinfo[] idr = temp3. GetDirectories ()//Gets all subdirectories under the current directory.

foreach  (DIRECTORYINFO&NBSP;DIR&NBSP;IN&NBSP;IDR)

Console.WriteLine (dir. FullName);

Fileinfo[] files = temp3. GetFiles ();

foreach  (fileinfo file in files)

Console.WriteLine (file. FullName);

Output C Tray catalog information:

 

In the program before and after the C disk directory screenshot, the comparison of changes in the situation, more than a atest directory.

Two. file reading and writing

C # and Java for the operation of the file is similar to the idea, are the use of pipeline technology and flow operation-stream to achieve the corresponding functions. When you start manipulating files, you use a file-specific stream: The FileStream in the System.IO namespace. There are a lot of constructors, Now let's introduce a very common approach:

FileStream Constructors (String, FileMode);

FileMode are: Open,create,openorcreate,createnew.append, truncate and other modes.

Let's look at one example:

FileInfo mynewfile = new FileInfo ("C:\\myfile.txt");

FileStream SF = new FileStream (mynewfile. ToString (), filemode.create);//constructor, open the destination file in Create mode, and create the appropriate file if it does not exist.

byte[] DataArray = new byte[100000];//byte

New Random (). Nextbytes (DataArray);//Create random bytes

for (int i = 0; i < dataarray.length; i++)

{

sf. WriteByte (Dataarray[i]);//writes bytes to the file.

}

Other construction methods are:

I will not introduce you here, you can refer to the MSDN file. There is a better streamwriter/and StreamReader about file reading and writing. The next section will continue.

The Fielstream class operates on byte and byte arrays, and character data is easy to use, but some operations, such as random file access (data that accesses a point in the middle of a file), must be performed by the FileStream object.

The Fielstream class maintains an internal file pointer that points to the location of the next read and write operation in the file.

FileStream can only handle raw bytes (raw byte), and the ability to handle raw bytes can be used to process any data file using the FileStream class.

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.