1 // Copyright 2004-2007 Castle Project - http://www.castleproject.org/
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
15 namespace Castle
.Core
.Resource
21 /// Enable access to files on network shares
23 public class UncResource
: AbstractStreamResource
25 private readonly Stream stream
;
26 private String basePath
;
27 private string filePath
;
29 public UncResource(CustomUri resource
)
31 stream
= CreateStreamFromUri(resource
, DefaultBasePath
);
34 public UncResource(CustomUri resource
, String basePath
)
36 stream
= CreateStreamFromUri(resource
, basePath
);
39 public UncResource(String resourceName
) : this(new CustomUri(resourceName
))
43 public UncResource(String resourceName
, String basePath
) : this(new CustomUri(resourceName
), basePath
)
47 public override String FileBasePath
49 get { return basePath; }
52 public override IResource
CreateRelative(String resourceName
)
54 return new UncResource(Path
.Combine(basePath
, resourceName
));
57 public override string ToString()
59 return String
.Format("UncResource: [{0}] [{1}]", filePath
, basePath
);
62 protected override Stream Stream
64 get { return stream; }
67 private Stream
CreateStreamFromUri(CustomUri resource
, String basePath
)
70 throw new ArgumentNullException("resource");
72 throw new ArgumentException("Resource must be an Unc", "resource");
74 throw new ArgumentException("The specified resource is not a file", "resource");
76 String filePath
= resource
.Path
;
78 if (!File
.Exists(filePath
) && basePath
!= null)
80 filePath
= Path
.Combine(basePath
, filePath
);
83 this.filePath
= Path
.GetFileName(filePath
);
84 this.basePath
= Path
.GetDirectoryName(filePath
);
86 CheckFileExists(filePath
);
88 return File
.OpenRead(filePath
);
91 private void CheckFileExists(String path
)
93 if (!File
.Exists(path
))
95 String message
= String
.Format("File {0} could not be found", path
);
96 throw new ResourceException(message
);