forked from fo-dicom/fo-dicom
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleaned up TemporaryFile class. Issue fo-dicom#69
- Loading branch information
1 parent
57204f2
commit 586a23e
Showing
8 changed files
with
109 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,96 +1,73 @@ | ||
// Copyright (c) 2012-2015 fo-dicom contributors. | ||
// Licensed under the Microsoft Public License (MS-PL). | ||
|
||
using System; | ||
using System.IO; | ||
|
||
namespace Dicom.IO | ||
{ | ||
public class TemporaryFile : IDisposable | ||
{ | ||
private string _file; | ||
|
||
public TemporaryFile() | ||
{ | ||
_file = Create(); | ||
} | ||
|
||
~TemporaryFile() | ||
{ | ||
TemporaryFileRemover.Delete(_file); | ||
} | ||
using System; | ||
|
||
public void Dispose() | ||
{ | ||
TemporaryFileRemover.Delete(_file); | ||
GC.SuppressFinalize(this); | ||
} | ||
/// <summary> | ||
/// Support class for creating a temporary file. | ||
/// </summary> | ||
public static class TemporaryFile | ||
{ | ||
#region FIELDS | ||
|
||
public string Name | ||
{ | ||
get | ||
{ | ||
return _file; | ||
} | ||
} | ||
private static string storagePath; | ||
|
||
#region Static | ||
#endregion | ||
|
||
private static string _path = null; | ||
#region PROPERTIES | ||
|
||
/// <summary> | ||
/// Gets or sets the directory location of the temporary files. | ||
/// </summary> | ||
public static string StoragePath | ||
{ | ||
get | ||
{ | ||
if (_path != null) return _path; | ||
if (storagePath != null) return storagePath; | ||
return IOManager.Path.GetTempDirectory(); | ||
} | ||
set | ||
{ | ||
_path = value; | ||
if (_path != null) | ||
storagePath = value; | ||
if (storagePath != null) | ||
{ | ||
var directory = IOManager.CreateDirectoryReference(_path); | ||
var directory = IOManager.CreateDirectoryReference(storagePath); | ||
if (!directory.Exists) directory.Create(); | ||
} | ||
} | ||
} | ||
|
||
public static string Create() | ||
#endregion | ||
|
||
#region METHODS | ||
|
||
/// <summary> | ||
/// Creates a temporary file and returns its name. | ||
/// </summary> | ||
/// <returns>Name of the temporary file.</returns> | ||
public static IFileReference Create() | ||
{ | ||
string path = null; | ||
IFileReference file; | ||
|
||
if (_path != null) | ||
if (storagePath != null) | ||
{ | ||
// create file in user specified path | ||
path = IOManager.Path.Combine(_path, Guid.NewGuid().ToString()); | ||
File.Create(path).Close(); | ||
var path = IOManager.Path.Combine(storagePath, Guid.NewGuid().ToString()); | ||
file = IOManager.CreateFileReference(path); | ||
file.Create().Dispose(); | ||
} | ||
else | ||
{ | ||
// allow OS to create file in system temp path | ||
path = IOManager.Path.GetTempFileName(); | ||
} | ||
|
||
try | ||
{ | ||
// set temporary file attribute so that the file system | ||
// will attempt to keep all of the data in memory | ||
File.SetAttributes(path, File.GetAttributes(path) | FileAttributes.Temporary); | ||
} | ||
catch | ||
{ | ||
// sometimes fails with invalid argument exception | ||
file = IOManager.CreateFileReference(IOManager.Path.GetTempFileName()); | ||
} | ||
file.IsTempFile = true; | ||
|
||
return path; | ||
return file; | ||
} | ||
|
||
#endregion | ||
|
||
public override string ToString() | ||
{ | ||
return String.Format("{0} [TEMP]", Name); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters