Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using Microsoft.Win32.SafeHandles;
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Diagnostics;
- using System.Drawing;
- using System.IO;
- using System.Linq;
- using System.Runtime.InteropServices;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows.Forms;
- namespace Лаба__4
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private string fileSrc = "test.txt";
- private string fileDest = "test_copy.txt";
- public class Win32
- {
- IntPtr handle;
- const uint GENERIC_READ = 0x80000000;
- public const uint GENERIC_WRITE = 0x40000000;
- const uint OPEN_EXISTING = 3;
- const uint CREATE_ALWAYS = 2;
- [DllImport("kernel32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Unicode)]
- public static extern unsafe IntPtr CreateFile
- (
- string FileName, // file name
- uint DesiredAccess, // access mode
- uint ShareMode, // share mode
- uint SecurityAttributes, // Security Attributes
- uint CreationDisposition, // how to create
- uint FlagsAndAttributes, // file attributes
- int hTemplateFile // handle to template file
- );
- [DllImport("kernel32.dll", SetLastError = true)]
- static extern unsafe bool ReadFile
- (
- IntPtr hFile, // handle to file
- void* pBuffer, // data buffer
- int NumberOfBytesToRead, // number of bytes to read
- int* pNumberOfBytesRead, // number of bytes read
- int Overlapped // overlapped buffer
- );
- [DllImport("kernel32.dll", SetLastError = true)]
- static extern unsafe bool WriteFile(
- IntPtr hFile,
- void* lpBuffer,
- int nNumberOfBytesToWrite,
- int* lpNumberOfBytesWritten,
- int lpOverlapped);
- [DllImport("kernel32.dll", SetLastError = true)]
- public static extern unsafe bool CloseHandle
- (
- IntPtr hObject // handle to object
- );
- public bool Open(string FileName)
- {
- // open the existing file for reading
- handle = CreateFile
- (
- FileName,
- GENERIC_READ,
- 0,
- 0,
- OPEN_EXISTING,
- 0,
- 0
- );
- if (handle != IntPtr.Zero)
- {
- return true;
- }
- else
- {
- return false;
- }
- }
- public unsafe int Read(byte[] buffer, int index, int count)
- {
- int n = 0;
- fixed (byte* p = buffer)
- {
- if (!ReadFile(handle, p + index, count, &n, 0))
- {
- return 0;
- }
- }
- return n;
- }
- public unsafe int Write(IntPtr handleFile, byte[] bufferData, int index, int length)
- {
- int n = 0;
- fixed (byte* w = bufferData)
- {
- if (!WriteFile(handleFile, w + index, length, &n, 0))
- {
- return 0;
- }
- }
- return n;
- }
- public bool Close()
- {
- return CloseHandle(handle);
- }
- }
- private void button2_Click(object sender, EventArgs e)
- {
- Close();
- }
- private void button1_Click(object sender, EventArgs e)
- {
- textBox1.AppendText("Создание файла... \r\n");
- CreateFile(Convert.ToInt32(numericUpDown1.Value));
- if (checkBox1.Checked)
- {
- textBox1.AppendText("Затраченное время на первый способ: " + FileVariable() + " миллисекунд \r\n");
- }
- if (checkBox2.Checked)
- {
- textBox1.AppendText("Затраченное время на второй способ: " + WinAPIFunc() + " миллисекунд \r\n");
- }
- textBox1.AppendText("\r\n");
- progressBar1.Value = 0;
- }
- private void CreateFile(int lenght)
- {
- byte[] buffer = new byte[1024]; //1Kb buffer
- int i;
- progressBar1.Maximum = lenght * 1024 * 1024;
- using (FileStream fs = new FileStream(fileSrc, FileMode.Create, FileAccess.Write))
- {
- for (i = 1; i <= lenght * 1024 * 1024; i += buffer.Length)
- {
- fs.Write(buffer, 0, buffer.Length);
- progressBar1.Value = i;
- }
- fs.Close();
- }
- }
- private int FileVariable()
- {
- int start, finish, result;
- progressBar1.Value = 0;
- progressBar1.Maximum = 100;
- start = DateTime.Now.Millisecond;
- File.Copy(fileSrc, fileDest, true);
- finish = DateTime.Now.Millisecond;
- progressBar1.Value = 70;
- result = finish - start;
- progressBar1.Value = 100;
- return result;
- }
- private int WinAPIFunc()
- {
- int start, finish, result;
- Win32 api = new Win32();
- if (api.Open(fileSrc))
- {
- start = DateTime.Now.Millisecond;
- byte[] buffer = new byte[1024];
- IntPtr h = Win32.CreateFile(fileDest, Win32.GENERIC_WRITE, 0, 0, 2, 0, 0);
- Debug.WriteLine(h.ToString());
- int bytesRead;
- do
- {
- bytesRead = api.Read(buffer, 0, buffer.Length);
- if (bytesRead != 0)
- {
- int wr = api.Write(h, buffer, 0, bytesRead);
- }
- }
- while (bytesRead > 0);
- api.Close();
- Win32.CloseHandle(h);
- finish = DateTime.Now.Millisecond;
- result = finish - start;
- return result;
- }
- return 0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement