1 // Copyright 2004-2008 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 String basePath
;
26 private string filePath
;
28 public UncResource(CustomUri resource
)
30 CreateStream
= delegate
32 return CreateStreamFromUri(resource
, DefaultBasePath
);
36 public UncResource(CustomUri resource
, String basePath
)
38 CreateStream
= delegate
40 return CreateStreamFromUri(resource
, basePath
);
44 public UncResource(String resourceName
) : this(new CustomUri(resourceName
))
48 public UncResource(String resourceName
, String basePath
) : this(new CustomUri(resourceName
), basePath
)
52 public override String FileBasePath
54 get { return basePath; }
57 public override IResource
CreateRelative(String resourceName
)
59 return new UncResource(Path
.Combine(basePath
, resourceName
));
62 public override string ToString()
64 return String
.Format("UncResource: [{0}] [{1}]", filePath
, basePath
);
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
);