Skip to content

Commit b1d0b6c

Browse files
committed
Fixed potential crash on attempted cleanup of temporary files on exit.
1 parent 73bbc98 commit b1d0b6c

1 file changed

Lines changed: 35 additions & 29 deletions

File tree

VidCoder/Utilities/FileCleanup.cs

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -83,48 +83,54 @@ public static void CleanOldLogs()
8383

8484
public static void CleanHandBrakeTempFiles()
8585
{
86-
DirectoryInfo tempDirectoryInfo = new(Path.GetTempPath());
87-
DirectoryInfo[] handBrakeTempDirectories = tempDirectoryInfo.GetDirectories("hb.*");
88-
89-
if (handBrakeTempDirectories.Length == 0)
86+
try
9087
{
91-
return;
92-
}
88+
DirectoryInfo tempDirectoryInfo = new(Path.GetTempPath());
89+
DirectoryInfo[] handBrakeTempDirectories = tempDirectoryInfo.GetDirectories("hb.*");
9390

94-
95-
foreach (DirectoryInfo handBrakeTempDirectory in handBrakeTempDirectories)
96-
{
97-
bool deleteFolder = false;
98-
try
99-
{
100-
string processIdString = handBrakeTempDirectory.Name.Substring(3);
101-
var process = Process.GetProcessById(int.Parse(processIdString, CultureInfo.InvariantCulture));
102-
if (process.HasExited)
103-
{
104-
deleteFolder = true;
105-
}
106-
}
107-
catch (ArgumentException)
91+
if (handBrakeTempDirectories.Length == 0)
10892
{
109-
// If there is no process with that ID running anymore, attempt cleanup on the temp files.
110-
deleteFolder = true;
111-
}
112-
catch (Exception)
113-
{
114-
// Do not attempt cleanup.
93+
return;
11594
}
11695

117-
if (deleteFolder)
96+
foreach (DirectoryInfo handBrakeTempDirectory in handBrakeTempDirectories)
11897
{
98+
bool deleteFolder = false;
11999
try
120100
{
121-
FileUtilities.DeleteDirectory(handBrakeTempDirectory.FullName);
101+
string processIdString = handBrakeTempDirectory.Name.Substring(3);
102+
var process = Process.GetProcessById(int.Parse(processIdString, CultureInfo.InvariantCulture));
103+
if (process.HasExited)
104+
{
105+
deleteFolder = true;
106+
}
107+
}
108+
catch (ArgumentException)
109+
{
110+
// If there is no process with that ID running anymore, attempt cleanup on the temp files.
111+
deleteFolder = true;
122112
}
123113
catch (Exception)
124114
{
125-
// If cleanup fails, will be attempted next time.
115+
// Do not attempt cleanup.
116+
}
117+
118+
if (deleteFolder)
119+
{
120+
try
121+
{
122+
FileUtilities.DeleteDirectory(handBrakeTempDirectory.FullName);
123+
}
124+
catch (Exception)
125+
{
126+
// If cleanup fails, will be attempted next time.
127+
}
126128
}
127129
}
128130
}
131+
catch (Exception)
132+
{
133+
// If cleanup fails, will be attempted next time.
134+
}
129135
}
130136
}

0 commit comments

Comments
 (0)