C # file operations (upload, download, and delete a file list ...)

Source: Internet
Author: User

Using system. IO;

1. File Upload
----------
Key points:
HTML section:
<Form ID = "form1" runat = "server" method = "Post" enctype = "multipart/form-Data">
<Input id = "fileupload" type = "file" runat = "server"/> <br/>
Backend CS button events
// String strfilefullname = system. Io. Path. getfilename (this. fileupload. postedfile. filename );
// This. fileupload. postedfile. saveas (server. mappath ("./xmlzip/") + strfilefullname );

2. File Download
----------
Set the download connection for the selectedindexchanged event of ListBox.
Protected void lst_downloadfilelist_selectedindexchanged (Object sender, eventargs E)
{
Try
{
String strjs = "window. Open ('xmlzip /";
Strjs + = This. lst_downloadfilelist.selecteditem.text.trim ();
Strjs + = "'); Return false ;";
This. imgbtn_downloadfile.attributes.add ("onclick", strjs );
}
Catch (exception ex)
{
Ex. tostring ();
}
}
Alternatively, you can change the text value of the label to achieve the super connection for file download after clicking.
This. label1.text = "<a href = \" xmlzip/a.rar \ "> a.rar </a>"

3. File Deletion
---------
String strfilepath = server. mappath ("../countryflowmgr/xmlzip/" + this. lst_downloadfilelist.selecteditem.text.trim ());
If (file. exists (strfilepath ))
{
File. Delete (strfilepath );
If (file. exists (strfilepath ))
{
Response. Write ("OK ");
}
Else
{
Response. Write ("OK ");
}
}

4. Obtain the file list in the folder.
-----------
# Region get the list of currently available files
/// <Summary>
/// Obtain the list of currently available files
/// </Summary>
/// <Param name = "isalert"> whether a prompt is required </param>
Private void fn_getcurrfilelist (bool isalert)
{
Try
{
// Find the relevant ZIP file that belongs to unitcoding in the xmlzip folder.
String strxmlzipdirectory = server. mappath ("../xmlzip /");
If (directory. exists (strxmlzipdirectory ))
{
// Directoryinfo di = new directoryinfo (environment. currentdirectory );
Directoryinfo di = new directoryinfo (strxmlzipdirectory );

Fileinfo [] Fi = Di. getfiles ("*. Zip"); // check only. ZIP files
If (Fi. length> 0)
{
Lst_downloadfilelist.items.clear ();
Foreach (fileinfo tmpfi in FI)
{
Listitem tmpitem = new listitem ();
Tmpitem. Text = tmpfi. Name;
Lst_downloadfilelist.items.add (tmpitem );
}
Lst_downloadfilelist.selectedindex = 0;
}
Else
{
If (isalert)
{
Response. Write ("no files can be downloaded! ");
}
}
}
}
Catch (exception ex)
{
Ex. tostring ();
}
}
# Endregion
More details will be added later

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.