Beispiel #1
0
 public IStorageItem GetItem(string path, StorageTypes itemType = StorageTypes.All,bool createIfNotExisted=false)
 {
     
     var absolutePath = new AbsolutePath(path, this);
     if (itemType.IsDirectory())
     {
         if (System.IO.Directory.Exists(absolutePath))
         {
             return new StorageDirectory(new DirectoryInfo(absolutePath), null, absolutePath.Storage);
         }
         else if(createIfNotExisted){
             var dir = new DirectoryInfo(absolutePath);
             dir.Create();
             return new StorageDirectory(dir, null, absolutePath.Storage);
         }
     }
     if (itemType.IsFile())
     {
         if (System.IO.File.Exists(absolutePath))
         {
             return new StorageFile(new FileInfo(absolutePath), null, absolutePath.Storage);
         }
         else if(createIfNotExisted){
             var info = new FileInfo(absolutePath);
             if (!info.Directory.Exists) info.Directory.Create();
             using (var s = info.Create()) { };
             return new StorageFile(info, null, absolutePath.Storage);
         }
     }
     return null;
 }
Beispiel #2
0
 public bool Delete(string path) {
     var absolutePath = new AbsolutePath(path, this);
     var item = this.GetItem(path);
     if (item != null) {
         if (item.StorageType == StorageTypes.Directory) (item as StorageDirectory).Delete();
         else (item as StorageFile).Delete();
         return true;
     }
     return false;
 }
Beispiel #3
0
 public IStorageItem CreateItem(string path, StorageTypes itemType = StorageTypes.Directory)
 {
     //CreateFilePathIfNotExisted(path);
     var filename = new AbsolutePath(path,this);
     if (itemType == StorageTypes.Directory)
     {
         var dirInfo = System.IO.Directory.CreateDirectory(filename);
         return new StorageDirectory(dirInfo,null, filename.Storage);
     }
     if (itemType == StorageTypes.File)
     {
         var info = new FileInfo(filename);
         System.IO.Directory.CreateDirectory(info.DirectoryName);
         using (info.Create()) { } ;
         
         return new StorageFile(info,null,filename.Storage);
     }
     return null;
 }
Beispiel #4
0
 public void PutBytes(string path, byte[] bytes) {
     var filename = new AbsolutePath(path, this);
     var info = new FileInfo(filename);
     if (!info.Exists)
     {
         if (!info.Directory.Exists) info.Directory.Create();
     }
     
     System.IO.File.WriteAllBytes(filename,bytes);
 }
Beispiel #5
0
        public IList<IStorageItem> ListItems(string path, StorageTypes itemType = StorageTypes.All)
        {
            
            var filename = new AbsolutePath(path , this);

            if (!System.IO.Directory.Exists(filename)) return null;
            var result = new List<IStorageItem>();
            if (itemType.IsFile())
            {
                var filenames = System.IO.Directory.GetFiles(filename);
                foreach(var name in filenames) result.Add(new StorageFile(new FileInfo(name),this,filename.Storage));
            }
            if (itemType.IsDirectory())
            {
                path += "/";
                var filenames = System.IO.Directory.GetDirectories(filename);
                foreach (var name in filenames) result.Add(new StorageDirectory(new DirectoryInfo(name),this,filename.Storage));
            }
            return result;
        }
Beispiel #6
0
 public byte[] GetBytes(string path) {
     var filename = new AbsolutePath(path, this);
     if (!System.IO.File.Exists(filename)) return null;
     return System.IO.File.ReadAllBytes(filename);
 }
Beispiel #7
0
 public async Task AppendTextAsync(string path, string text, Encoding encoding = null)
 {
     var filename = new AbsolutePath(path, this);
     var info = new FileInfo(filename);
     if (!info.Exists)
     {
         if (!info.Directory.Exists) info.Directory.Create();
     }
     if (encoding == null) encoding = System.Text.Encoding.GetEncoding(Constants.DefaultCodepage);
     var buffer = encoding.GetBytes(text);
     using (var stream = System.IO.File.OpenWrite(filename))
     {
         stream.Seek(0, System.IO.SeekOrigin.End);
         await stream.WriteAsync(buffer, 0, buffer.Length);
     }
 }