Skip to content

Commit c6120ab

Browse files
committed
additional methods in StorageInstance
1 parent 098f0fd commit c6120ab

File tree

1 file changed

+61
-1
lines changed

1 file changed

+61
-1
lines changed

LabExtended/Core/Storage/StorageInstance.cs

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ public bool Add(StorageValue value)
256256
if (lookup.ContainsKey(value.Name))
257257
return false;
258258

259-
value.Path = System.IO.Path.GetFullPath(System.IO.Path.Combine(Path, value.Name));
259+
value.Path = GetPath(value.Name);
260260

261261
value.Storage = this;
262262
value.OnAdded();
@@ -272,6 +272,66 @@ public bool Add(StorageValue value)
272272
return true;
273273
}
274274

275+
/// <summary>
276+
/// Determines whether a file with the specified name exists.
277+
/// </summary>
278+
/// <param name="name">The name of the file to check for existence. Cannot be null or empty.</param>
279+
/// <returns>true if a file with the specified name exists; otherwise, false.</returns>
280+
/// <exception cref="ArgumentNullException">Thrown if name is null or empty.</exception>
281+
public bool Exists(string name)
282+
{
283+
if (string.IsNullOrEmpty(name))
284+
throw new ArgumentNullException(nameof(name));
285+
286+
return File.Exists(GetPath(name));
287+
}
288+
289+
/// <summary>
290+
/// Combines the specified file or directory name with the current base path and returns the absolute path.
291+
/// </summary>
292+
/// <param name="name">The file or directory name to combine with the base path. Cannot be null or empty.</param>
293+
/// <returns>The absolute path resulting from combining the base path with the specified name.</returns>
294+
/// <exception cref="ArgumentNullException">Thrown if <paramref name="name"/> is null or empty.</exception>
295+
public string GetPath(string name)
296+
{
297+
if (string.IsNullOrEmpty(name))
298+
throw new ArgumentNullException(nameof(name));
299+
300+
return System.IO.Path.GetFullPath(System.IO.Path.Combine(Path, name));
301+
}
302+
303+
/// <summary>
304+
/// Removes the entry with the specified name from the collection, and optionally deletes the associated file
305+
/// from disk.
306+
/// </summary>
307+
/// <param name="name">The name of the entry to remove. Cannot be null, empty, or consist only of white-space characters.</param>
308+
/// <param name="deleteFile">true to delete the associated file from disk if it exists; otherwise, false. The default is false.</param>
309+
/// <returns>true if the entry or its associated file was successfully removed; otherwise, false.</returns>
310+
/// <exception cref="ArgumentNullException">Thrown if name is null, empty, or consists only of white-space characters.</exception>
311+
public bool Remove(string name, bool deleteFile = false)
312+
{
313+
if (string.IsNullOrWhiteSpace(name))
314+
throw new ArgumentNullException(nameof(name));
315+
316+
var valueRemoved = lookup.TryGetValue(name, out var value) && Remove(value, deleteFile);
317+
318+
if (valueRemoved)
319+
return true;
320+
321+
if (deleteFile)
322+
{
323+
var path = GetPath(name);
324+
325+
if (File.Exists(path))
326+
{
327+
File.Delete(path);
328+
return true;
329+
}
330+
}
331+
332+
return false;
333+
}
334+
275335
/// <summary>
276336
/// Removes the specified <see cref="StorageValue"/> from the collection and optionally deletes its associated
277337
/// file.

0 commit comments

Comments
 (0)