C # download File Delete file Write text

Source: Internet
Author: User

Due to the frequent use of file processing, they have encapsulated the next share to everyone. Contains the write text bulk delete file download file. --can be used directly

//<summary>
//write to TXT
//</summary>
//<param name= "Savepath" ></PARAM>
<param name= "Content" ></PARAM>
public static void Writeintxt (string savepath, string content)
{ br> string temppath = System.IO.Path.GetDirectoryName (Savepath);
System.IO.Directory.CreateDirectory (TempPath);//create temporary file directory
if (! System.IO.File.Exists (Savepath))
{
FileStream FS1 = new FileStream (Savepath, FileMode.Create, FileAccess.Write );//Create Write file
StreamWriter sw = new StreamWriter (FS1);
SW. WriteLine (content);//start writing value
SW. Close ();
FS1. Close ();
}
Else
{
FileStream fs = new FileStream (Savepath, FileMode.Open, FileAccess.Write);
StreamWriter sr = new StreamWriter (FS);
Sr. WriteLine (content);//Start writing value
Sr. Close ();
FS. Close ();
}
}

<summary>
Recursively delete all files under a folder
</summary>
<param name= "File" ></param>
public static void DeleteFile (String dirpath)
{
Try
{
Remove read-only properties for folders and sub-files
Remove read-only properties for a folder
System.IO.DirectoryInfo fileInfo = new DirectoryInfo (Dirpath);
Fileinfo.attributes = Fileattributes.normal & fileattributes.directory;
To remove a read-only property of a file
System.IO.File.SetAttributes (Dirpath, System.IO.FileAttributes.Normal);
Determine if a folder still exists
if (directory.exists (Dirpath))
{
foreach (string f in Directory.getfilesystementries (Dirpath))
{
if (File.exists (f))
{
If you have a child file to delete a file
File.delete (f);
}
Else
{
Looping recursively deleting subfolders
DeleteFile (f);
}
}
Delete Empty folders
Directory.delete (Dirpath);
}
}
catch (Exception e)
{

}
}

<summary>
HTTP Download file
</summary>
<param name= "url" > download file path </param>
<param name= "Savepath" > Save path </param>
<returns></returns>
public static bool Httpdownloadfile (string URL, string savepath)
{
String TempPath = System.IO.Path.GetDirectoryName (Savepath);
System.IO.Directory.CreateDirectory (TempPath); Create a temporary file directory
String tempfile = TempPath + @ "\" + System.IO.Path.GetFileName (Savepath); Temporary files
if (System.IO.File.Exists (tempfile))
{
exist then jump out
return true;
System.IO.File.Delete (Tempfile);
}
Try
{
FileStream fs = new FileStream (Tempfile, Filemode.append, FileAccess.Write, fileshare.readwrite);
Setting parameters
HttpWebRequest request = webrequest.create (URL) as HttpWebRequest;
Send request and get corresponding response data
HttpWebResponse response = Request. GetResponse () as HttpWebResponse;
Until request. The GetResponse () program only starts sending a POST request to the destination Web page
Stream Responsestream = Response. GetResponseStream ();
Creating a local file write stream
Stream stream = new FileStream (tempfile, FileMode.Create);
byte[] BArr = new byte[1024];
int size = Responsestream.read (BARR, 0, (int) barr.length);
while (Size > 0)
{
Stream. Write (BARR, 0, size);
Fs. Write (BARR, 0, size);
Size = Responsestream.read (BARR, 0, (int) barr.length);
}
Stream. Close ();
Fs. Close ();
Responsestream.close ();
System.IO.File.Move (Tempfile, Savepath);
return true;
}
catch (Exception ex)
{
return false;
}
}

C # download File Delete file Write text

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.