Skip to content

Commit 156cda6

Browse files
Sébastien GeiserSébastien Geiser
Sébastien Geiser
authored and
Sébastien Geiser
committed
Handles locked Excel files by creating copies
Addresses the issue where the application fails to open Excel files if they are locked by another process. Implements a mechanism to create a temporary copy of the Excel file if the original is locked, allowing the application to proceed without interruption. This ensures continued functionality even when the Excel file is in use elsewhere.
1 parent a5feb5c commit 156cda6

File tree

4 files changed

+52
-5
lines changed

4 files changed

+52
-5
lines changed

RegexDialog/Model/ExcelSheetSelection.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using ClosedXML.Excel;
22
using DocumentFormat.OpenXml.Bibliography;
33
using Newtonsoft.Json;
4+
using RegexDialog.Utils;
45
using System;
56
using System.Collections.Generic;
67
using System.Linq;
@@ -25,7 +26,7 @@ public class ExcelSheetSelection : NotifyPropertyChangedBaseClass
2526
[JsonIgnore]
2627
public ICommand EvaluateCommand => new RelayCommand(_ =>
2728
{
28-
using(IXLWorkbook workbook = new XLWorkbook(Config.Instance.TextSourceExcelPath))
29+
using(IXLWorkbook workbook = new XLWorkbook(Config.Instance.TextSourceExcelPath.MakeCopyIfLocked()))
2930
{
3031
try
3132
{

RegexDialog/RegExToolDialog.xaml.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Newtonsoft.Json;
99
using Ookii.Dialogs.Wpf;
1010
using RegexDialog.Behaviors;
11+
using RegexDialog.Utils;
1112
using System;
1213
using System.Collections.Generic;
1314
using System.Collections.ObjectModel;
@@ -702,7 +703,7 @@ List<RegexResult> GetMatchesFor(string text, string fileName = "", int selection
702703
}
703704
else if (Config.Instance.TextSourceOn == RegexTextSource.Excel)
704705
{
705-
using (XLWorkbook workbook = new(Config.Instance.TextSourceExcelPath))
706+
using (XLWorkbook workbook = new(Config.Instance.TextSourceExcelPath.MakeCopyIfLocked()))
706707
{
707708
var elementNb = 0;
708709
MatchResultsTreeView.ItemsSource = Config.Instance.ExcelSheets
@@ -1143,7 +1144,7 @@ void Extract(string text, string fileName = "")
11431144
}
11441145
else if (Config.Instance.TextSourceOn == RegexTextSource.Excel)
11451146
{
1146-
using (XLWorkbook workbook = new(Config.Instance.TextSourceExcelPath))
1147+
using (XLWorkbook workbook = new(Config.Instance.TextSourceExcelPath.MakeCopyIfLocked()))
11471148
{
11481149
Config.Instance.ExcelSheets
11491150
.FindAll(sheetSelection => sheetSelection.IsSelected)
@@ -1257,7 +1258,7 @@ private void IsMatch()
12571258
}
12581259
else if (Config.Instance.TextSourceOn == RegexTextSource.Excel)
12591260
{
1260-
using (XLWorkbook workbook = new(Config.Instance.TextSourceExcelPath))
1261+
using (XLWorkbook workbook = new(Config.Instance.TextSourceExcelPath.MakeCopyIfLocked()))
12611262
{
12621263
bool found = false;
12631264
string sheetName = string.Empty;

RegexDialog/Utils/Config.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using ClosedXML.Excel;
22
using Newtonsoft.Json;
33
using Newtonsoft.Json.Converters;
4+
using RegexDialog.Utils;
45
using System;
56
using System.Collections.Generic;
67
using System.Collections.ObjectModel;
@@ -188,7 +189,7 @@ public void OnTextSourceExcelPathChanged()
188189
isInit = false;
189190
try
190191
{
191-
using (XLWorkbook workbook = new(TextSourceExcelPath))
192+
using (XLWorkbook workbook = new(TextSourceExcelPath.MakeCopyIfLocked()))
192193
{
193194
ExcelSheets.ForEach(sheetSelection => sheetSelection.PropertyChanged -= SubPropertyChanged_PropertyChanged);
194195

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
using System.IO;
2+
3+
namespace RegexDialog.Utils
4+
{
5+
public static class PotentiallyLockedFileCopier
6+
{
7+
public static string MakeCopyIfLocked(this string sourceFile)
8+
{
9+
if(IsFileLocked(new FileInfo(sourceFile)))
10+
{
11+
string destinationFile = Path.Combine(Path.GetTempPath(), Path.GetFileName(sourceFile));
12+
13+
if (!File.Exists(destinationFile) || File.GetLastWriteTime(sourceFile) != File.GetLastWriteTime(destinationFile))
14+
{
15+
File.Copy(sourceFile, destinationFile);
16+
}
17+
18+
return destinationFile;
19+
20+
}
21+
else
22+
{
23+
return sourceFile;
24+
}
25+
}
26+
27+
private static bool IsFileLocked(FileInfo file)
28+
{
29+
try
30+
{
31+
using (FileStream stream = file.Open(FileMode.Open, FileAccess.Read, FileShare.None))
32+
{
33+
stream.Close();
34+
}
35+
}
36+
catch (IOException)
37+
{
38+
return true;
39+
}
40+
41+
return false;
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)