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)
{
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