Advertisement
Guest User

Лаба

a guest
Oct 26th, 2016
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.82 KB | None | 0 0
  1. using Microsoft.Win32.SafeHandles;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Diagnostics;
  7. using System.Drawing;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Runtime.InteropServices;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using System.Windows.Forms;
  14.  
  15. namespace Лаба__4
  16. {
  17.     public partial class Form1 : Form
  18.     {
  19.         public Form1()
  20.         {
  21.             InitializeComponent();
  22.         }
  23.         private string fileSrc = "test.txt";
  24.         private string fileDest = "test_copy.txt";
  25.  
  26.         public class Win32
  27.         {
  28.             IntPtr handle;
  29.             const uint GENERIC_READ = 0x80000000;
  30.             public const uint GENERIC_WRITE = 0x40000000;
  31.             const uint OPEN_EXISTING = 3;
  32.             const uint CREATE_ALWAYS = 2;
  33.  
  34.             [DllImport("kernel32.dll", SetLastError = true, ThrowOnUnmappableChar = true, CharSet = CharSet.Unicode)]
  35.             public static extern unsafe IntPtr CreateFile
  36.             (
  37.                 string FileName,          // file name
  38.                 uint DesiredAccess,       // access mode
  39.                 uint ShareMode,           // share mode
  40.                 uint SecurityAttributes,  // Security Attributes
  41.                 uint CreationDisposition, // how to create
  42.                 uint FlagsAndAttributes,  // file attributes
  43.                 int hTemplateFile         // handle to template file
  44.             );
  45.  
  46.             [DllImport("kernel32.dll", SetLastError = true)]
  47.             public static extern unsafe bool ReadFile
  48.             (
  49.                 IntPtr hFile,      // handle to file
  50.                 void* pBuffer,            // data buffer
  51.                 int NumberOfBytesToRead,  // number of bytes to read
  52.                 int* pNumberOfBytesRead,  // number of bytes read
  53.                 int Overlapped            // overlapped buffer
  54.             );
  55.  
  56.             [DllImport("kernel32.dll", SetLastError = true)]
  57.             public static extern unsafe bool WriteFile(
  58.                 IntPtr hFile,
  59.                 void* lpBuffer,
  60.                 int nNumberOfBytesToWrite,
  61.                 int* lpNumberOfBytesWritten,
  62.                 int lpOverlapped);
  63.  
  64.             [DllImport("kernel32.dll", SetLastError = true)]
  65.             public static extern unsafe bool CloseHandle
  66.             (
  67.                 IntPtr hObject // handle to object
  68.             );
  69.  
  70.             public bool Open(string FileName)
  71.             {
  72.                 // open the existing file for reading      
  73.                 handle = CreateFile
  74.                 (
  75.                     FileName,
  76.                     GENERIC_READ,
  77.                     0,
  78.                     0,
  79.                     OPEN_EXISTING,
  80.                     0,
  81.                     0
  82.                 );
  83.  
  84.                 if (handle != IntPtr.Zero)
  85.                 {
  86.                     return true;
  87.                 }
  88.                 else
  89.                 {
  90.                     return false;
  91.                 }
  92.             }
  93.  
  94.             public unsafe int Read(byte[] buffer, int index, int count)
  95.             {
  96.                 int n = 0;
  97.                 fixed (byte* p = buffer)
  98.                 {
  99.                     if (!ReadFile(handle, p + index, count, &n, 0))
  100.                     {
  101.                         return 0;
  102.                     }
  103.                 }
  104.                 return n;
  105.             }
  106.  
  107.             public unsafe int Write(IntPtr handleFile, byte[] bufferData, int index, int length)
  108.             {
  109.                 int n = 0;
  110.                 fixed (byte* w = bufferData)
  111.                 {
  112.                     if (!WriteFile(handleFile, w + index, length, &n, 0))
  113.                     {
  114.                         return 0;
  115.                     }
  116.                 }
  117.                 return n;
  118.             }
  119.  
  120.             public bool Close()
  121.             {
  122.                 return CloseHandle(handle);
  123.             }
  124.  
  125.         }
  126.  
  127.         private void button2_Click(object sender, EventArgs e)
  128.         {
  129.             Close();
  130.         }
  131.  
  132.         private void button1_Click(object sender, EventArgs e)
  133.         {
  134.             textBox1.AppendText("Создание файла... \r\n");
  135.             CreateFile(Convert.ToInt32(numericUpDown1.Value));
  136.             if (checkBox1.Checked)
  137.             {
  138.                 textBox1.AppendText("Затраченное время на первый способ: " + FileVariable() + " миллисекунд \r\n");
  139.             }
  140.             if (checkBox2.Checked)
  141.             {
  142.                 textBox1.AppendText("Затраченное время на второй способ: " + WinAPIFunc() + " миллисекунд \r\n");
  143.             }
  144.             textBox1.AppendText("\r\n");
  145.             progressBar1.Value = 0;
  146.         }
  147.  
  148.         private void CreateFile(int lenght)
  149.         {
  150.             byte[] buffer = new byte[1024]; //1Kb buffer
  151.             int i;
  152.             progressBar1.Maximum = lenght * 1024 * 1024;
  153.             using (FileStream fs = new FileStream(fileSrc, FileMode.Create, FileAccess.Write))
  154.             {
  155.                 for (i = 1; i <= lenght * 1024 * 1024; i += buffer.Length)
  156.                 {
  157.                     fs.Write(buffer, 0, buffer.Length);
  158.                     progressBar1.Value = i;
  159.                 }
  160.                 fs.Close();
  161.             }
  162.  
  163.         }
  164.  
  165.         private int FileVariable()
  166.         {
  167.             int start, finish, result;
  168.             progressBar1.Value = 0;
  169.             progressBar1.Maximum = 100;
  170.             start = DateTime.Now.Millisecond;
  171.             File.Copy(fileSrc, fileDest, true);
  172.             finish = DateTime.Now.Millisecond;
  173.             progressBar1.Value = 70;
  174.             result = finish - start;
  175.             progressBar1.Value = 100;
  176.             return result;
  177.         }
  178.  
  179.         private int WinAPIFunc()
  180.         {
  181.             int start, finish, result;
  182.             Win32 api = new Win32();
  183.             if (api.Open(fileSrc))
  184.             {
  185.                 start = DateTime.Now.Millisecond;
  186.                 byte[] buffer = new byte[1024];
  187.                 IntPtr h = Win32.CreateFile(fileDest, Win32.GENERIC_WRITE, 0, 0, 2, 0, 0);
  188.                 Debug.WriteLine(h.ToString());
  189.                 int bytesRead;
  190.                 do
  191.                 {
  192.                     bytesRead = api.Read(buffer, 0, buffer.Length);
  193.                     if (bytesRead != 0)
  194.                     {
  195.                         int wr = api.Write(h, buffer, 0, bytesRead);
  196.                     }
  197.                 }
  198.                 while (bytesRead > 0);
  199.                 api.Close();
  200.                 Win32.CloseHandle(h);
  201.                 finish = DateTime.Now.Millisecond;
  202.                 result = finish - start;
  203.                 return result;
  204.             }
  205.             return 0;
  206.         }
  207.     }
  208. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement