File and folder operations

Source: Internet
Author: User
C # operations on files and folders Author: Author name Source: this site for sorting release: 12:50:39

1. Get the folder list

/// <Summary>
/// Obtain the file list in the folder
/// </Summary>
/// <Param name = "path"> string path: folder path (@ "C: \") </param>
/// <Param name = "searchpattern"> string searchpattern: Extension filtering ("*. txt") </param>
/// <Param name = "searchchild"> bool searchchild: if it is set to false, a subdirectory is not searched. If it is set to true, a subdirectory is searched. </param>
/// <Returns> </returns>

Public String [] getfilelist (string path, string searchpattern, bool searchchild)
{
// Determine whether a folder exists
If (directory. exists (PATH) = false)
{
Return NULL;
}
If (searchchild ){
// Search for subdirectories
Return directory. getfiles (path, searchpattern, searchoption. alldirectories );
}
Else
{
// Do not search for subdirectories
Return directory. getfiles (path, searchpattern, searchoption. topdirectoryonly );
}
}

Ii. Open a file

/// <Summary>
/// Open the file
/// </Summary>
/// <Param name = "path"> string path: file path </param>
/// <Returns> </returns>
Public System. Diagnostics. Process openfile (string path)
{
If (file. exists (PATH) = true)
{// Open the file. By default, the file is opened by the software associated with the system registration type.
Return System. Diagnostics. process. Start (PATH );
}
Else
{
Return NULL;
}
}


* You can perform some operations on opened files:

Public System. Diagnostics. Process P1;

P1 = openfile (@ "C: \ 1.txt ");

P1.. Kill (); // close the opened file

P1.waitforexit (); // wait until the opened file is closed

P1.mainwindowtitle; // obtain the title of the Main Window for opening the file

P1.hasexited; // determine whether the opened file is closed

3. Get File Information

///


// obtain the File Information
///
/// string path: file Path
//
Public fileinfo getfileinfo (string path)
{< br> If (file. exists (PATH) = true)
{< br> return New fileinfo (PATH);

}
Else
{
Return NULL;
}
}

* The usage is as follows:

Fileinfo fi1 = getfileinfo (@ "F: \ 1.txt ");

Fi1.extension; // get the file extension

Fi1.directoryname; // obtain the complete directory of the file

Fi1.length; // get the number of bytes of the object

Fi1.name; // get the file name, including the extension

Fi1.name. Replace (f1.extension, ""); // get the file name, excluding the extension

4. Create a folder

/// <Summary>
/// Create a folder
/// </Summary>
/// <Param name = "path"> string path: folder path </param>
/// <Returns> </returns>
Public bool createdir (string path)
{
Try
{// Determine whether a folder exists
If (directory. exists (PATH) = false)
{
Directory. createdirectory (PATH );
Return true;
}
Else
{
Return false;
}

}
Catch
{
Return false;
}
}

5. Generate a file using a binary array
/// <Summary>
/// Use a binary array byte [] to generate an object
/// </Summary>
/// <Param name = "filefullpath"> string path: full path of the file to be generated </param>
/// <Param name = "streambyte"> byte [] streambyte: the binary byte array of the file to be generated </param>
/// <Returns> </returns>
Public bool bytestreamtofile (string path, byte [] streambyte)
{
Try
{// Determine whether the file to be created exists. If yes, delete it first.
If (file. exists (PATH) = true)
{
File. Delete (PATH );
}
// Create a file stream (open or created)
Filestream FS = new filestream (path, filemode. openorcreate );
// Write the file stream to the file
FS. Write (streambyte, 0, streambyte. Length );
FS. Close ();
Return true;
}
Catch
{
Return false;
}
}

6. Obtain system folders

Environment. getfolderpath (environment. specialfolder. startup)

Note:

Applicationdata Directory, which is used as the application of the current roaming user Program Public repository for specific data.
The commonapplicationdata Directory, which is used as a public repository of application-specific data for all users.
Commonprogramfiles is the directory of components shared between applications.
The directory where cookies are used as the public repository of Internet cookies.
Desktop logical desktop, rather than the location of the physical file system.
Topdirectory is used to physically store the directory of file objects on the desktop.
Favorites is used as the directory of the public repository for user favorites items.
History is used as the directory of the public repository for Internet history items.
The directory where internetcache is used as a public repository for Internet temporary files.
Localapplicationdata Directory, which is used as a public repository of application-specific data for non-roaming users.
Mycomputer "my computer" folder.
Mymusic "My music" folder.
Mypictures "my pictures" folder.
For personal Composition Directory of the public repository.
ProgramFiles "Program Files" directory.
Programs contains the directory of the user program group.
Recent contains the directory of the most recently used documents.
Sendto directory that contains the "send" menu item.
Startmenu contains the directory of the "Start" menu item.
Startup corresponds to the directory of the user's "start" program group.
System "system" directory.
The directory where templates is used as the public repository for document templates.

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.