Browse Source

commit: pre allocate before write; taskbar progress support

HOME 3 years ago
parent
commit
1c4c78eb70

+ 6 - 0
NuGet.config

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8"?> 
+<configuration> 
+  <config> 
+    <add key="repositorypath" value="C:\NuGetLocalRepo" /> 
+  </config> 
+</configuration>

+ 36 - 21
StrangeFileCopy/MainForm.Designer.cs

@@ -43,6 +43,7 @@
             this.CurrentOpLabel = new System.Windows.Forms.Label();
             this.CurrentOperationLabel = new System.Windows.Forms.Label();
             this.ConfigGroupBox = new System.Windows.Forms.GroupBox();
+            this.MaxCheckBox = new System.Windows.Forms.CheckBox();
             this.FlushCheckBox = new System.Windows.Forms.CheckBox();
             this.ReOpenSizeUnDown = new System.Windows.Forms.NumericUpDown();
             this.ReOpenGbLabel = new System.Windows.Forms.Label();
@@ -51,7 +52,7 @@
             this.BufSizeMbLabel = new System.Windows.Forms.Label();
             this.BufSizeLabel = new System.Windows.Forms.Label();
             this.UiUpdateTimer = new System.Windows.Forms.Timer(this.components);
-            this.MaxCheckBox = new System.Windows.Forms.CheckBox();
+            this.PauseCheckBox = new System.Windows.Forms.CheckBox();
             this.ConfigGroupBox.SuspendLayout();
             ((System.ComponentModel.ISupportInitialize)(this.ReOpenSizeUnDown)).BeginInit();
             ((System.ComponentModel.ISupportInitialize)(this.BufSizeUpDown)).BeginInit();
@@ -63,7 +64,7 @@
             | System.Windows.Forms.AnchorStyles.Right)));
             this.TotalProgressBar.Location = new System.Drawing.Point(75, 221);
             this.TotalProgressBar.Name = "TotalProgressBar";
-            this.TotalProgressBar.Size = new System.Drawing.Size(485, 23);
+            this.TotalProgressBar.Size = new System.Drawing.Size(497, 23);
             this.TotalProgressBar.TabIndex = 0;
             // 
             // ReOpenProgressBar
@@ -72,7 +73,7 @@
             | System.Windows.Forms.AnchorStyles.Right)));
             this.ReOpenProgressBar.Location = new System.Drawing.Point(75, 192);
             this.ReOpenProgressBar.Name = "ReOpenProgressBar";
-            this.ReOpenProgressBar.Size = new System.Drawing.Size(485, 23);
+            this.ReOpenProgressBar.Size = new System.Drawing.Size(497, 23);
             this.ReOpenProgressBar.TabIndex = 0;
             // 
             // FromLabel
@@ -91,7 +92,7 @@
             this.FromTextBox.Location = new System.Drawing.Point(75, 77);
             this.FromTextBox.Name = "FromTextBox";
             this.FromTextBox.ReadOnly = true;
-            this.FromTextBox.Size = new System.Drawing.Size(404, 21);
+            this.FromTextBox.Size = new System.Drawing.Size(416, 21);
             this.FromTextBox.TabIndex = 0;
             this.FromTextBox.DragDrop += new System.Windows.Forms.DragEventHandler(this.FromTextBox_DragDrop);
             this.FromTextBox.DragEnter += new System.Windows.Forms.DragEventHandler(this.FromTextBox_DragEnter);
@@ -104,7 +105,7 @@
             this.ToTextBox.Location = new System.Drawing.Point(75, 104);
             this.ToTextBox.Name = "ToTextBox";
             this.ToTextBox.ReadOnly = true;
-            this.ToTextBox.Size = new System.Drawing.Size(404, 21);
+            this.ToTextBox.Size = new System.Drawing.Size(416, 21);
             this.ToTextBox.TabIndex = 1;
             this.ToTextBox.DragDrop += new System.Windows.Forms.DragEventHandler(this.ToTextBox_DragDrop);
             this.ToTextBox.DragEnter += new System.Windows.Forms.DragEventHandler(this.ToTextBox_DragEnter);
@@ -120,7 +121,7 @@
             // FireButton
             // 
             this.FireButton.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
-            this.FireButton.Location = new System.Drawing.Point(485, 77);
+            this.FireButton.Location = new System.Drawing.Point(497, 77);
             this.FireButton.Name = "FireButton";
             this.FireButton.Size = new System.Drawing.Size(75, 48);
             this.FireButton.TabIndex = 2;
@@ -152,7 +153,7 @@
             | System.Windows.Forms.AnchorStyles.Right)));
             this.BufProgressBar.Location = new System.Drawing.Point(75, 163);
             this.BufProgressBar.Name = "BufProgressBar";
-            this.BufProgressBar.Size = new System.Drawing.Size(485, 23);
+            this.BufProgressBar.Size = new System.Drawing.Size(497, 23);
             this.BufProgressBar.TabIndex = 0;
             // 
             // BufLabel
@@ -196,11 +197,24 @@
             this.ConfigGroupBox.Controls.Add(this.BufSizeLabel);
             this.ConfigGroupBox.Location = new System.Drawing.Point(12, 12);
             this.ConfigGroupBox.Name = "ConfigGroupBox";
-            this.ConfigGroupBox.Size = new System.Drawing.Size(548, 59);
+            this.ConfigGroupBox.Size = new System.Drawing.Size(560, 59);
             this.ConfigGroupBox.TabIndex = 4;
             this.ConfigGroupBox.TabStop = false;
             this.ConfigGroupBox.Text = "Config";
             // 
+            // MaxCheckBox
+            // 
+            this.MaxCheckBox.AutoSize = true;
+            this.MaxCheckBox.Checked = true;
+            this.MaxCheckBox.CheckState = System.Windows.Forms.CheckState.Checked;
+            this.MaxCheckBox.Location = new System.Drawing.Point(156, 23);
+            this.MaxCheckBox.Name = "MaxCheckBox";
+            this.MaxCheckBox.Size = new System.Drawing.Size(84, 16);
+            this.MaxCheckBox.TabIndex = 1;
+            this.MaxCheckBox.Text = "Max 2G-56B";
+            this.MaxCheckBox.UseVisualStyleBackColor = true;
+            this.MaxCheckBox.CheckedChanged += new System.EventHandler(this.MaxCheckBox_CheckedChanged);
+            // 
             // FlushCheckBox
             // 
             this.FlushCheckBox.AutoSize = true;
@@ -303,25 +317,25 @@
             this.UiUpdateTimer.Enabled = true;
             this.UiUpdateTimer.Tick += new System.EventHandler(this.UiUpdateTimer_Tick);
             // 
-            // MaxCheckBox
+            // PauseCheckBox
             // 
-            this.MaxCheckBox.AutoSize = true;
-            this.MaxCheckBox.Checked = true;
-            this.MaxCheckBox.CheckState = System.Windows.Forms.CheckState.Checked;
-            this.MaxCheckBox.Location = new System.Drawing.Point(156, 23);
-            this.MaxCheckBox.Name = "MaxCheckBox";
-            this.MaxCheckBox.Size = new System.Drawing.Size(84, 16);
-            this.MaxCheckBox.TabIndex = 1;
-            this.MaxCheckBox.Text = "Max 2G-56B";
-            this.MaxCheckBox.UseVisualStyleBackColor = true;
-            this.MaxCheckBox.CheckedChanged += new System.EventHandler(this.MaxCheckBox_CheckedChanged);
+            this.PauseCheckBox.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
+            this.PauseCheckBox.AutoSize = true;
+            this.PauseCheckBox.Location = new System.Drawing.Point(507, 137);
+            this.PauseCheckBox.Name = "PauseCheckBox";
+            this.PauseCheckBox.Size = new System.Drawing.Size(54, 16);
+            this.PauseCheckBox.TabIndex = 1;
+            this.PauseCheckBox.Text = "Pause";
+            this.PauseCheckBox.UseVisualStyleBackColor = true;
+            this.PauseCheckBox.CheckedChanged += new System.EventHandler(this.PauseCheckBox_CheckedChanged);
             // 
             // MainForm
             // 
             this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
             this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
-            this.ClientSize = new System.Drawing.Size(572, 261);
+            this.ClientSize = new System.Drawing.Size(584, 261);
             this.Controls.Add(this.ConfigGroupBox);
+            this.Controls.Add(this.PauseCheckBox);
             this.Controls.Add(this.FireButton);
             this.Controls.Add(this.ToTextBox);
             this.Controls.Add(this.FromTextBox);
@@ -337,7 +351,7 @@
             this.Controls.Add(this.TotalProgressBar);
             this.MaximizeBox = false;
             this.MaximumSize = new System.Drawing.Size(99999, 300);
-            this.MinimumSize = new System.Drawing.Size(450, 300);
+            this.MinimumSize = new System.Drawing.Size(600, 300);
             this.Name = "MainForm";
             this.Text = "Strange File Copy";
             this.ConfigGroupBox.ResumeLayout(false);
@@ -374,6 +388,7 @@
         private System.Windows.Forms.Label ReOpenSizeLabel;
         private System.Windows.Forms.Timer UiUpdateTimer;
         private System.Windows.Forms.CheckBox MaxCheckBox;
+        private System.Windows.Forms.CheckBox PauseCheckBox;
     }
 }
 

+ 50 - 17
StrangeFileCopy/MainForm.cs

@@ -1,4 +1,5 @@
-using System;
+using Microsoft.WindowsAPICodePack.Taskbar;
+using System;
 using System.IO;
 using System.Threading;
 using System.Windows.Forms;
@@ -11,11 +12,10 @@ namespace StrangeFileCopy
         private int _bufProgress;
         private int _reOpenProgress;
         private int _totalProgress;
+        private bool _pause;
+        private bool _error;
 
-        public MainForm()
-        {
-            InitializeComponent();
-        }
+        public MainForm() => InitializeComponent();
 
         private void FireButton_Click(object sender, EventArgs e)
         {
@@ -29,7 +29,7 @@ namespace StrangeFileCopy
             }
 
             var blkSize = MaxCheckBox.Checked ? 2147483591 : (int)BufSizeUpDown.Value * 1024 * 1024;
-            var reOpenTimes = (int)Math.Ceiling(ReOpenSizeUnDown.Value * 1024 / BufSizeUpDown.Value);
+            var reOpenTimes = (int)Math.Ceiling(ReOpenSizeUnDown.Value * 1024 * 1024 * 1024 / blkSize);
             var flush = FlushCheckBox.Checked;
 
             FireButton.Enabled = false;
@@ -44,13 +44,24 @@ namespace StrangeFileCopy
                     _status = "Opening Source";
                     src = File.OpenRead(srcPath);
                     var srcLen = src.Length;
+
+                    _status = "Pre allocating Dest - Opening";
+                    dst = File.OpenWrite(dstPath);
+                    _status = "Pre allocating Dest - Seeking";
+                    dst.Position = src.Length - 1;
+                    _status = "Pre allocating Dest - Writing";
+                    dst.WriteByte(0);
+                    _status = "Pre allocating Dest - Flushing";
+                    dst.Flush(true);
+                    _status = "Pre allocating Dest - Closing";
+                    dst.Close();
+
                     long pos = 0;
 
                     var buf = new byte[blkSize];
 
                     while (pos < srcLen)
                     {
-                        //open dest and set position
                         _status = "Opening Dest";
                         dst = File.OpenWrite(dstPath);
                         dst.Position = pos;
@@ -77,6 +88,15 @@ namespace StrangeFileCopy
                             _reOpenProgress = (int)(100 * (float)i / reOpenTimes);
                             _totalProgress = (int)(100 * (float)src.Position / srcLen);
 
+                            if (_pause)
+                            {
+                                _status = "Paused";
+                                while (_pause)
+                                {
+                                    Thread.Sleep(250);
+                                }
+                            }
+
                             if (src.Position == srcLen) break;
                         }
 
@@ -89,11 +109,19 @@ namespace StrangeFileCopy
                 catch (Exception exception)
                 {
                     _status = $"Error:{exception.Message}";
+                    _error = true;
                 }
                 finally
                 {
                     src?.Close();
-                    dst?.Close();
+                    try
+                    {
+                        dst?.Close();
+                    }
+                    catch
+                    {
+                        //ignore
+                    }
                 }
             }).Start();
         }
@@ -104,6 +132,15 @@ namespace StrangeFileCopy
             BufProgressBar.Value = _bufProgress;
             ReOpenProgressBar.Value = _reOpenProgress;
             TotalProgressBar.Value = _totalProgress;
+            if (_error)
+            {
+                TaskbarManager.Instance.SetProgressState(TaskbarProgressBarState.Error);
+            }
+            else
+            {
+                TaskbarManager.Instance.SetProgressState(_pause ? TaskbarProgressBarState.Paused : TaskbarProgressBarState.Normal);
+                TaskbarManager.Instance.SetProgressValue(_totalProgress, 100);
+            }
         }
 
         private void FromTextBox_DragEnter(object sender, DragEventArgs e)
@@ -116,10 +153,7 @@ namespace StrangeFileCopy
                 : DragDropEffects.None;
         }
 
-        private void FromTextBox_DragDrop(object sender, DragEventArgs e)
-        {
-            FromTextBox.Text = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
-        }
+        private void FromTextBox_DragDrop(object sender, DragEventArgs e) => FromTextBox.Text = ((string[])e.Data.GetData(DataFormats.FileDrop))[0];
 
         private void ToTextBox_DragEnter(object sender, DragEventArgs e)
         {
@@ -149,7 +183,7 @@ namespace StrangeFileCopy
                     var dest = Path.Combine(to, filename);
                     if (File.Exists(dest))
                     {
-                        if (DialogResult.Yes == MessageBox.Show("Target file exist,override it?"))
+                        if (DialogResult.Yes == MessageBox.Show("Target file exist,override it?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2))
                         {
                             ToTextBox.Text = dest;
                         }
@@ -166,9 +200,8 @@ namespace StrangeFileCopy
             }).Start();
         }
 
-        private void MaxCheckBox_CheckedChanged(object sender, EventArgs e)
-        {
-            BufSizeUpDown.Enabled = !MaxCheckBox.Checked;
-        }
+        private void MaxCheckBox_CheckedChanged(object sender, EventArgs e) => BufSizeUpDown.Enabled = !MaxCheckBox.Checked;
+
+        private void PauseCheckBox_CheckedChanged(object sender, EventArgs e) => _pause = PauseCheckBox.Checked;
     }
 }

+ 30 - 0
StrangeFileCopy/StrangeFileCopy.csproj

@@ -1,5 +1,6 @@
 <?xml version="1.0" encoding="utf-8"?>
 <Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="C:\NuGetLocalRepo\ILRepack.2.0.18\build\ILRepack.props" Condition="Exists('C:\NuGetLocalRepo\ILRepack.2.0.18\build\ILRepack.props')" />
   <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
   <PropertyGroup>
     <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
@@ -12,6 +13,8 @@
     <FileAlignment>512</FileAlignment>
     <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
     <Deterministic>true</Deterministic>
+    <NuGetPackageImportStamp>
+    </NuGetPackageImportStamp>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
     <PlatformTarget>AnyCPU</PlatformTarget>
@@ -33,7 +36,16 @@
     <WarningLevel>4</WarningLevel>
     <Prefer32Bit>false</Prefer32Bit>
   </PropertyGroup>
+  <PropertyGroup>
+    <RunPostBuildEvent>OnOutputUpdated</RunPostBuildEvent>
+  </PropertyGroup>
   <ItemGroup>
+    <Reference Include="Microsoft.WindowsAPICodePack, Version=1.1.0.0, Culture=neutral, processorArchitecture=MSIL">
+      <HintPath>C:\NuGetLocalRepo\WindowsAPICodePack-Core.1.1.1\lib\Microsoft.WindowsAPICodePack.dll</HintPath>
+    </Reference>
+    <Reference Include="Microsoft.WindowsAPICodePack.Shell, Version=1.1.0.0, Culture=neutral, processorArchitecture=MSIL">
+      <HintPath>C:\NuGetLocalRepo\WindowsAPICodePack-Shell.1.1.1\lib\Microsoft.WindowsAPICodePack.Shell.dll</HintPath>
+    </Reference>
     <Reference Include="System" />
     <Reference Include="System.Core" />
     <Reference Include="System.Xml.Linq" />
@@ -61,6 +73,24 @@
   </ItemGroup>
   <ItemGroup>
     <None Include="App.config" />
+    <None Include="packages.config" />
   </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+  <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
+    <PropertyGroup>
+      <ErrorText>这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。</ErrorText>
+    </PropertyGroup>
+    <Error Condition="!Exists('C:\NuGetLocalRepo\ILRepack.2.0.18\build\ILRepack.props')" Text="$([System.String]::Format('$(ErrorText)', 'C:\NuGetLocalRepo\ILRepack.2.0.18\build\ILRepack.props'))" />
+  </Target>
+  <PropertyGroup>
+    <PostBuildEvent>if $(ConfigurationName) == Release if not exist "$(TargetDir)Packed" md "$(TargetDir)Packed"
+if $(ConfigurationName) == Release setlocal enabledelayedexpansion enableextensions
+if $(ConfigurationName) == Release set DLL_LIST=
+if $(ConfigurationName) == Release for %25%25x in ($(TargetDir)*.dll) do set DLL_LIST=!DLL_LIST! "%25%25x"
+if $(ConfigurationName) == Release echo dlls: !DLL_LIST!
+if $(ConfigurationName) == Release $(ILRepack) /ndebug "/out:$(TargetDir)Packed\$(TargetFileName)" "$(TargetPath)" !DLL_LIST!
+if $(ConfigurationName) == Release if exist "$(TargetDir)Packed\$(TargetFileName).config" del "$(TargetDir)Packed\$(TargetFileName).config"
+
+</PostBuildEvent>
+  </PropertyGroup>
 </Project>

+ 6 - 0
StrangeFileCopy/packages.config

@@ -0,0 +1,6 @@
+<?xml version="1.0" encoding="utf-8"?>
+<packages>
+  <package id="ILRepack" version="2.0.18" targetFramework="net461" />
+  <package id="WindowsAPICodePack-Core" version="1.1.1" targetFramework="net461" />
+  <package id="WindowsAPICodePack-Shell" version="1.1.1" targetFramework="net461" />
+</packages>

+ 5 - 0
StrangeTools.sln

@@ -5,6 +5,11 @@ VisualStudioVersion = 16.0.29920.165
 MinimumVisualStudioVersion = 10.0.40219.1
 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StrangeFileCopy", "StrangeFileCopy\StrangeFileCopy.csproj", "{7E8C017E-78B6-4AC1-B042-1BDFAF461EE7}"
 EndProject
+Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "@", "@", "{8E093C14-C04A-45F1-951A-19A5BCBAF703}"
+	ProjectSection(SolutionItems) = preProject
+		NuGet.config = NuGet.config
+	EndProjectSection
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU