Skip to content

Commit 22ca927

Browse files
author
zzzprojects
committed
Add KeepModifiedate + Bette Handle console
Add KeepModifiedate + Bette Handle console
1 parent 54b169e commit 22ca927

17 files changed

+304
-110
lines changed

src/FindAndReplace.App/CommandLineOptions.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,17 @@ public class CommandLineOptions
7272
[Option("useEscapeChars", HelpText = "Escape special chars.")]
7373
public bool UseEscapeChars { get; set; }
7474

75+
[Option("KeepModifiedDate", HelpText = "Keep Modified Date.")]
76+
public bool IsKeepModifiedDate { get; set; }
7577

76-
#endregion
78+
79+
80+
81+
#endregion
7782

78-
#region Specialized Option Attribute
83+
#region Specialized Option Attribute
7984

80-
[HelpOption("help", HelpText = "Display this help screen.")]
85+
[HelpOption("help", HelpText = "Display this help screen.")]
8186
public string GetUsage()
8287
{
8388
var help = new HelpText("Find And Replace");

src/FindAndReplace.App/FindAndReplace.App.csproj

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
<TargetFrameworkProfile>
1515
</TargetFrameworkProfile>
1616
<FileAlignment>512</FileAlignment>
17+
<IsWebBootstrapper>false</IsWebBootstrapper>
18+
<SccProjectName>SAK</SccProjectName>
19+
<SccLocalPath>SAK</SccLocalPath>
20+
<SccAuxPath>SAK</SccAuxPath>
21+
<SccProvider>SAK</SccProvider>
1722
<PublishUrl>publish\</PublishUrl>
1823
<Install>true</Install>
1924
<InstallFrom>Disk</InstallFrom>
@@ -26,13 +31,8 @@
2631
<MapFileExtensions>true</MapFileExtensions>
2732
<ApplicationRevision>0</ApplicationRevision>
2833
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
29-
<IsWebBootstrapper>false</IsWebBootstrapper>
3034
<UseApplicationTrust>false</UseApplicationTrust>
3135
<BootstrapperEnabled>true</BootstrapperEnabled>
32-
<SccProjectName>SAK</SccProjectName>
33-
<SccLocalPath>SAK</SccLocalPath>
34-
<SccAuxPath>SAK</SccAuxPath>
35-
<SccProvider>SAK</SccProvider>
3636
</PropertyGroup>
3737
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|x86' ">
3838
<PlatformTarget>AnyCPU</PlatformTarget>
@@ -83,6 +83,7 @@
8383
<Compile Include="MainForm.Designer.cs">
8484
<DependentUpon>MainForm.cs</DependentUpon>
8585
</Compile>
86+
<Compile Include="ProcessExtension.cs" />
8687
<Compile Include="Program.cs" />
8788
<Compile Include="Properties\AssemblyInfo.cs" />
8889
<Compile Include="FormData.cs" />

src/FindAndReplace.App/FormData.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ public class FormData
2121
public string ReplaceText { get; set; }
2222
public bool UseEscapeChars { get; set; }
2323
public string Encoding { get; set; }
24+
public bool IsKeepModifiedDate { get; set; }
2425

25-
private static readonly string _versionIndependentRegKey;
26+
private static readonly string _versionIndependentRegKey;
2627

2728
static FormData()
2829
{
@@ -57,7 +58,8 @@ public void SaveToRegistry()
5758
SaveValueToRegistry("ReplaceText", ReplaceText);
5859
SaveValueToRegistry("UseEscapeChars", UseEscapeChars.ToString());
5960
SaveValueToRegistry("Encoding", Encoding);
60-
}
61+
SaveValueToRegistry("IsKeepModifiedDate", IsKeepModifiedDate.ToString());
62+
}
6163

6264
public bool IsEmpty()
6365
{
@@ -82,7 +84,8 @@ public void LoadFromRegistry()
8284
ReplaceText = GetValueFromRegistry("ReplaceText");
8385
UseEscapeChars = GetValueFromRegistry("UseEscapeChars") == "True";
8486
Encoding = GetValueFromRegistry("Encoding");
85-
}
87+
IsKeepModifiedDate= GetValueFromRegistry("IsKeepModifiedDate") == "True";
88+
}
8689

8790

8891
private void SaveValueToRegistry(string name, string value)

src/FindAndReplace.App/MainForm.Designer.cs

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/FindAndReplace.App/MainForm.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ private void SaveToRegistry()
9696
data.UseEscapeChars = chkUseEscapeChars.Checked;
9797
data.Encoding = cmbEncoding.Text;
9898
data.ExcludeDir = txtExcludeDir.Text;
99+
data.IsKeepModifiedDate = chkKeepModifiedDate.Checked;
99100

100101
data.SaveToRegistry();
101102

@@ -504,7 +505,8 @@ private void ShowReplaceResult(Replacer.ReplaceResultItem replaceResultItem, Sta
504505
{
505506
string fileContent = string.Empty;
506507

507-
using (var sr = new StreamReader(replaceResultItem.FilePath, replaceResultItem.FileEncoding))
508+
509+
using (var sr = new StreamReader(replaceResultItem.FilePath, replaceResultItem.FileEncoding))
508510
{
509511
fileContent = sr.ReadToEnd();
510512
}
@@ -528,7 +530,7 @@ private void ShowReplaceResult(Replacer.ReplaceResultItem replaceResultItem, Sta
528530

529531
lblStatus.Text = "Processing " + stats.Files.Processed + " of " + stats.Files.Total + " files. Last file: " +
530532
replaceResultItem.FileRelativePath;
531-
;
533+
532534

533535
ShowStats(stats, true);
534536
}
@@ -896,8 +898,10 @@ private void InitWithRegistryData()
896898
chkShowEncoding.Checked = data.ShowEncoding;
897899
txtReplace.Text = data.ReplaceText;
898900
chkUseEscapeChars.Checked = data.UseEscapeChars;
901+
chkKeepModifiedDate.Checked = data.IsKeepModifiedDate;
902+
899903

900-
if (!string.IsNullOrEmpty(data.Encoding))
904+
if (!string.IsNullOrEmpty(data.Encoding))
901905
cmbEncoding.SelectedIndex = cmbEncoding.Items.IndexOf(data.Encoding);
902906
}
903907

@@ -1025,8 +1029,10 @@ private Replacer GetReplacer()
10251029
replacer.IncludeFilesWithoutMatches = chkIncludeFilesWithoutMatches.Checked;
10261030
replacer.ReplaceText = CleanRichBoxText(txtReplace.Text);
10271031
replacer.UseEscapeChars = chkUseEscapeChars.Checked;
1032+
replacer.IsKeepModifiedDate = chkKeepModifiedDate.Checked;
10281033

1029-
if (cmbEncoding.SelectedIndex > 0)
1034+
1035+
if (cmbEncoding.SelectedIndex > 0)
10301036
replacer.AlwaysUseEncoding = Utils.GetEncodingByName(cmbEncoding.Text);
10311037

10321038
return replacer;

src/FindAndReplace.App/MainForm.resx

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -849,16 +849,4 @@
849849
<metadata name="folderBrowserDialog1.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
850850
<value>147, 17</value>
851851
</metadata>
852-
<metadata name="toolTip_btnSwap.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
853-
<value>316, 17</value>
854-
</metadata>
855-
<metadata name="toolTip_chkIncludeFilesWithoutMatches.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
856-
<value>459, 17</value>
857-
</metadata>
858-
<metadata name="toolTip_chkSkipBinaryFileDetection.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
859-
<value>17, 56</value>
860-
</metadata>
861-
<metadata name="toolTip_chkShowEncoding.TrayLocation" type="System.Drawing.Point, System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a">
862-
<value>259, 56</value>
863-
</metadata>
864852
</root>
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Diagnostics;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace FindAndReplace.App
9+
{
10+
public static class ProcessExtensions
11+
{
12+
//https://stackoverflow.com/questions/394816/how-to-get-parent-process-in-net-in-managed-way
13+
private static string FindIndexedProcessName(int pid)
14+
{
15+
var processName = Process.GetProcessById(pid).ProcessName;
16+
var processesByName = Process.GetProcessesByName(processName);
17+
string processIndexdName = null;
18+
19+
for (var index = 0; index < processesByName.Length; index++)
20+
{
21+
processIndexdName = index == 0 ? processName : processName + "#" + index;
22+
var processId = new PerformanceCounter("Process", "ID Process", processIndexdName);
23+
if ((int)processId.NextValue() == pid)
24+
{
25+
return processIndexdName;
26+
}
27+
}
28+
29+
return processIndexdName;
30+
}
31+
32+
private static Process FindPidFromIndexedProcessName(string indexedProcessName)
33+
{
34+
var parentId = new PerformanceCounter("Process", "Creating Process ID", indexedProcessName);
35+
return Process.GetProcessById((int)parentId.NextValue());
36+
}
37+
38+
public static Process Parent(this Process process)
39+
{
40+
return FindPidFromIndexedProcessName(FindIndexedProcessName(process.Id));
41+
}
42+
}
43+
}

src/FindAndReplace.App/Program.cs

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,30 +42,21 @@ private static int Main(string[] args)
4242

4343
if (args.Length != 0 && args.Contains("--cl"))
4444
{
45-
//Get a pointer to the forground window. The idea here is that
46-
//IF the user is starting our application from an existing console
47-
//shell, that shell will be the uppermost window. We'll get it
48-
//and attach to it
49-
IntPtr ptr = GetForegroundWindow();
45+
//Get a parent procss for see where send the output.
46+
Process process = Process.GetProcessById(Process.GetCurrentProcess().Id).Parent();
5047

51-
int u;
48+
if (process.ProcessName == "cmd") //Is the uppermost window a cmd process?
49+
{
50+
AttachConsole(process.Id);
51+
}
52+
else
53+
{
54+
//no console AND we're in console mode ... create a new console.
5255

53-
GetWindowThreadProcessId(ptr, out u);
56+
AllocConsole();
57+
}
5458

55-
Process process = Process.GetProcessById(u);
56-
57-
if (process.ProcessName == "cmd") //Is the uppermost window a cmd process?
58-
{
59-
AttachConsole(process.Id);
60-
}
61-
else
62-
{
63-
//no console AND we're in console mode ... create a new console.
64-
65-
AllocConsole();
66-
}
67-
68-
var clRunner = new CommandLineRunner();
59+
var clRunner = new CommandLineRunner();
6960
int dosErrorLevel = clRunner.Run(args);
7061

7162
FreeConsole();
@@ -234,8 +225,9 @@ public int Run(string[] args)
234225
replacer.IsSilent = _options.Silent;
235226

236227
replacer.FileProcessed += OnReplacerFileProcessed;
228+
replacer.IsKeepModifiedDate = _options.IsKeepModifiedDate;
237229

238-
var replaceResult = replacer.Replace();
230+
var replaceResult = replacer.Replace();
239231

240232
if (_options.SetErrorLevelIfAnyFileErrors)
241233
if (replaceResult.Stats.Files.FailedToRead > 0 || replaceResult.Stats.Files.FailedToWrite > 0)

src/FindAndReplace.App/Properties/AssemblyInfo.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@
3232
// You can specify all the values or you can default the Build and Revision Numbers
3333
// by using the '*' as shown below:
3434
// [assembly: AssemblyVersion("1.0.*")]
35-
[assembly: AssemblyVersion("1.7.2")]
36-
[assembly: AssemblyFileVersion("1.7.2")]
35+
[assembly: AssemblyVersion("1.7.3")]
36+
[assembly: AssemblyFileVersion("1.7.3")]

src/FindAndReplace.Tests/DetectEncodingAccuracyTest.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
namespace FindAndReplace.Tests
1010
{
1111
[TestFixture]
12-
[Ignore]
1312
public class DetectEncodingAccuracyTest : TestBase
1413
{
1514

0 commit comments

Comments
 (0)