1 | #Region "Microsoft.VisualBasic::652e836aa26b48201bf557c4ff71967b, Microsoft.VisualBasic.Core\Extensions\IO\Path\TemporaryEnvironment.vb" |
2 | |
3 | ' Author: |
4 | ' |
5 | ' asuka (amethyst.asuka@gcmodeller.org) |
6 | ' xie (genetics@smrucc.org) |
7 | ' xieguigang (xie.guigang@live.com) |
8 | ' |
9 | ' Copyright (c) 2018 GPL3 Licensed |
10 | ' |
11 | ' |
12 | ' GNU GENERAL PUBLIC LICENSE (GPL3) |
13 | ' |
14 | ' |
15 | ' This program is free software: you can redistribute it and/or modify |
16 | ' it under the terms of the GNU General Public License as published by |
17 | ' the Free Software Foundation, either version 3 of the License, or |
18 | ' (at your option) any later version. |
19 | ' |
20 | ' This program is distributed in the hope that it will be useful, |
21 | ' but WITHOUT ANY WARRANTY; without even the implied warranty of |
22 | ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
23 | ' GNU General Public License for more details. |
24 | ' |
25 | ' You should have received a copy of the GNU General Public License |
26 | ' along with this program. If not, see <http://www.gnu.org/licenses/>. |
27 | |
28 | |
29 | |
30 | ' /********************************************************************************/ |
31 | |
32 | ' Summaries: |
33 | |
34 | ' Class TemporaryEnvironment |
35 | ' |
36 | ' Constructor: (+1 Overloads) Sub New |
37 | ' |
38 | ' Function: makeDirectoryExists |
39 | ' |
40 | ' Sub: (+2 Overloads) Dispose |
41 | ' |
42 | ' |
43 | ' /********************************************************************************/ |
44 | |
45 | #End Region |
46 | |
47 | Namespace FileIO |
48 | |
49 | ''' <summary> |
50 | ''' Workspace model |
51 | ''' |
52 | ''' 在<see cref="TemporaryEnvironment.Dispose()"/>的时候,文件夹会切换回来 |
53 | ''' </summary> |
54 | Public Class TemporaryEnvironment : Inherits Directory |
55 | Implements IDisposable |
56 | |
57 | ReadOnly previous$ |
58 | |
59 | ''' <summary> |
60 | ''' 如果不存在会创建目标文件夹 |
61 | ''' </summary> |
62 | ''' <param name="newLocation"></param> |
63 | Sub New(newLocation As String) |
64 | Call MyBase.New(DIR:=makeDirectoryExists(newLocation)) |
65 | |
66 | previous = App.CurrentDirectory |
67 | App.CurrentDirectory = newLocation |
68 | End Sub |
69 | |
70 | Private Shared Function makeDirectoryExists(dir As String) As String |
71 | If Not dir.DirectoryExists Then |
72 | Call dir.MkDIR |
73 | End If |
74 | |
75 | Return dir |
76 | End Function |
77 | |
78 | #Region "IDisposable Support" |
79 | Private disposedValue As Boolean ' To detect redundant calls |
80 | |
81 | ' IDisposable |
82 | Protected Overridable Sub Dispose(disposing As Boolean) |
83 | If Not disposedValue Then |
84 | If disposing Then |
85 | ' TODO: dispose managed state (managed objects). |
86 | App.CurrentDirectory = previous |
87 | End If |
88 | |
89 | ' TODO: free unmanaged resources (unmanaged objects) and override Finalize() below. |
90 | ' TODO: set large fields to null. |
91 | End If |
92 | disposedValue = True |
93 | End Sub |
94 | |
95 | ' TODO: override Finalize() only if Dispose(disposing As Boolean) above has code to free unmanaged resources. |
96 | 'Protected Overrides Sub Finalize() |
97 | ' ' Do not change this code. Put cleanup code in Dispose(disposing As Boolean) above. |
98 | ' Dispose(False) |
99 | ' MyBase.Finalize() |
100 | 'End Sub |
101 | |
102 | ' This code added by Visual Basic to correctly implement the disposable pattern. |
103 | Public Sub Dispose() Implements IDisposable.Dispose |
104 | ' Do not change this code. Put cleanup code in Dispose(disposing As Boolean) above. |
105 | Dispose(True) |
106 | ' TODO: uncomment the following line if Finalize() is overridden above. |
107 | ' GC.SuppressFinalize(Me) |
108 | End Sub |
109 | #End Region |
110 | End Class |
111 | End Namespace |