added additional implementation
[mono.git] / mcs / class / corlib / System.IO / FileInfo.cs
1 //------------------------------------------------------------------------------\r
2 // \r
3 // System.IO.FileInfo.cs \r
4 //\r
5 // Copyright (C) 2001 Moonlight Enterprises, All Rights Reserved\r
6 // \r
7 // Author:         Jim Richardson, develop@wtfo-guru.com\r
8 // Created:        Monday, August 13, 2001 \r
9 //\r
10 //------------------------------------------------------------------------------\r
11 \r
12 using System;
13 using System.Private;\r
14 using System.IO.Private;
15 using System.Diagnostics;
16 using System.Security.Permissions;
17 \r
18 namespace System.IO\r
19 {\r
20         /// <summary>\r
21         /// \r
22         /// </summary>\r
23         public sealed class FileInfo : FileSystemInfo\r
24         {\r
25                 public FileInfo(string fileName)\r
26                 {
27                         CheckArgument.Path(fileName, false);
28                         //LAMESPEC: Does not throw security exception in constructor
29                         OriginalPath = fileName;
30                 }\r
31
32                 private bool existsOnDisk(bool exNotFound, bool exIsDirectory)
33                 {
34                         bool bRetCode;
35                         
36                         try
37                         {
38                                 Refresh();
39                                 if((getAttributes() & FileAttributes.Directory) != 0)
40                                 {
41                                         if(exIsDirectory)
42                                         {
43                                                 throw new UnauthorizedAccessException();
44                                         }
45                                         bRetCode = false;
46                                 }
47                                 else
48                                 {
49                                         bRetCode = true;
50                                 }
51                         }
52                         catch(ArgumentException ex)                             
53                         {
54                                 Debug.WriteLine(ex); // eliminates not used warning
55                                 if(exNotFound)
56                                 {
57                                         throw new FileNotFoundException();
58                                 }
59                                 bRetCode = false;
60                         }
61                         return bRetCode;
62                 }
63                 
64                 public override bool Exists\r
65                 {
66                         get
67                         {
68                                 return existsOnDisk(false, false);      
69                         }
70                 }\r
71 \r
72                 public override string Name\r
73                 {\r
74                         get\r
75                         {
76                                 return Path.GetFileName(getPathName());\r
77                         }\r
78                 }\r
79 \r
80                 /// <summary>\r
81                 /// Gets the parent directory info\r
82                 /// </summary>\r
83                 public DirectoryInfo Directory\r
84                 {\r
85                         get\r
86                         {\r
87                                 return new DirectoryInfo(Path.GetDirectoryName(getPathName()));\r
88                         }\r
89                 }\r
90 \r
91                 /// <summary>\r
92                 /// Get the path of the file\r
93                 /// </summary>\r
94                 public string DirectoryName\r
95                 {\r
96                         get\r
97                         {\r
98                                 return Path.GetDirectoryName(getPathName());\r
99                         }\r
100                 }\r
101 \r
102                 /// <summary>\r
103                 /// Get the length of the file\r
104                 /// </summary>\r
105                 public long Length\r
106                 {\r
107                         get\r
108                         {
109                                 try
110                                 {
111                                         Refresh();
112                                 }
113                                 catch(ArgumentException ex)
114                                 {
115                                         Debug.WriteLine(ex); // eliminates not used compiler warning
116                                         throw new FileNotFoundException();
117                                 }
118                                 return status.st_size;
119                         }\r
120                 }\r
121                 
122                 public StreamWriter AppendText()\r
123                 {       // TODO: verify using correct FileMode here might be Create & Append
124                         return new StreamWriter(Open(FileMode.Append, FileAccess.Write));
125                 }\r
126                 \r
127                 public FileStream Create()\r
128                 {
129                         // TODO: verify using correct FileMode here
130                         return Open(FileMode.OpenOrCreate, FileAccess.ReadWrite);\r
131                 }\r
132 \r
133                 public StreamWriter CreateText()\r
134                 {       //TODO: According to doc even CreateText throws a file not found ex
135                         //      sounds suspicious so i'll have to check it out later
136                         //existsOnDisk(true, true); // throw not found, is directory
137                         return new StreamWriter(Open(FileMode.Create, FileAccess.Write));
138                 }\r
139                 \r
140                 public FileStream Open(FileMode mode)\r
141                 {\r
142                         return Open(mode, FileAccess.ReadWrite);\r
143                 }\r
144 \r
145                 public FileStream Open(FileMode mode, FileAccess access)\r
146                 {\r
147                         return Open(mode, access, FileShare.None);\r
148                 }
149                 
150                 public FileStream Open(FileMode mode, FileAccess access, FileShare share)\r
151                 {
152                         bool bExists = existsOnDisk(false, true); // throw is directory;
153                         string path = getPathName();
154                     CheckPermission.ModeAccess(mode, access, path, bExists);                    \r
155                         return new FileStream(path, mode, access, share);\r
156                 }\r
157 \r
158                 public FileStream OpenRead()\r
159                 {       // TODO: find out what default share should be\r
160                         return Open(FileMode.Open, FileAccess.Read, FileShare.Read);\r
161                 }\r
162 \r
163                 public StreamReader OpenText()\r
164                 {       // TODO: verify mode and access values
165                         return new StreamReader(Open(FileMode.OpenOrCreate, FileAccess.ReadWrite));
166                 }\r
167 \r
168                 public FileStream OpenWrite()\r
169                 {\r
170                         return Open(FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);\r
171                 }\r
172 \r
173                 public FileInfo CopyTo(string destFile)\r
174                 {\r
175                         return CopyTo(destFile, false);\r
176                 }\r
177 \r
178                 public FileInfo CopyTo(string destFile, bool bOverwrite)\r
179                 {       // TODO: Implement\r
180                         return null;\r
181                 }\r
182 \r
183                 public override void Delete()\r
184                 {
185                         existsOnDisk(true, true); // throw not found, is directory
186                         CheckPermission.Demand(FileIOPermissionAccess.AllAccess, getPathName());
187                         Wrapper.unlink(getPathName());
188                 }\r
189 \r
190                 public void MoveTo(string destName)\r
191                 {       // TODO: Implement\r
192                 }\r
193         }\r
194 }\r