Advertisement
RaFaeLs

File system by RaFaeL

Aug 23rd, 2012
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 9.00 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Text.RegularExpressions;
  6.  
  7. /*
  8.  * =========================================================================================================
  9.  * =                        Writed By RaFaeL ZIlberman, basic for file system.                             =
  10.  * =                   This script is have some bugs and writed just for learning                          =
  11.  * =========================================================================================================
  12.  */
  13.  
  14. namespace Files
  15. {
  16.     class Files
  17.     {
  18.         /*
  19.          * Create files
  20.          */
  21.         public static bool Create(string path)
  22.         {
  23.             if (System.IO.File.Exists(path))
  24.                 return false;
  25.  
  26.             System.IO.FileStream file = System.IO.File.Create(path);
  27.             file.Close();
  28.             SetValue(path, "path", path);
  29.  
  30.             return true;
  31.         }
  32.  
  33.         /*
  34.          * Empty files
  35.          */
  36.         public static bool Empty(string path)
  37.         {
  38.             if (!System.IO.File.Exists(path))
  39.                 return false;
  40.  
  41.             System.IO.File.WriteAllText(path, null);
  42.  
  43.             return true;
  44.         }
  45.  
  46.         /*
  47.          * Copy and Move files
  48.          */
  49.         public static bool Copy(string path, string newpath, bool destroyfile = true, bool applayfile = false)
  50.         {
  51.             if (!System.IO.File.Exists(path) || (destroyfile == false && System.IO.File.Exists(newpath)))
  52.                 return false;
  53.  
  54.             if (applayfile == true)
  55.             {
  56.                 System.IO.StreamWriter newfile = System.IO.File.AppendText(newpath);
  57.                 string file = System.IO.File.ReadAllText(path);
  58.  
  59.                 newfile.Write("\r\n" + file);
  60.                 newfile.Close();
  61.             }
  62.             else
  63.             {
  64.                 System.IO.File.Copy(path, newpath);
  65.             }
  66.  
  67.             return true;
  68.         }
  69.         public static bool Move(string path, string newpath, bool destroyfile = false, bool applayfile = false)
  70.         {
  71.             if (!System.IO.File.Exists(path) || (destroyfile == false && System.IO.File.Exists(newpath)))
  72.                 return false;
  73.  
  74.             if (applayfile == true)
  75.             {
  76.                 System.IO.StreamWriter newfile = System.IO.File.AppendText(newpath);
  77.                 string file = System.IO.File.ReadAllText(path);
  78.  
  79.                 newfile.Write("\r\n" + file);
  80.                 newfile.Close();
  81.                 System.IO.File.Delete(path);
  82.             }
  83.             else
  84.             {
  85.                 System.IO.File.Move(path, newpath);
  86.             }
  87.  
  88.             return true;
  89.         }
  90.  
  91.         /*
  92.          * Convet file to setting's file
  93.          */
  94.         public static bool AddPath(string path, string main = null)
  95.         {
  96.             if (!System.IO.File.Exists(path))
  97.                 return false;
  98.  
  99.             return SetValue(path, "path", path);
  100.         }
  101.  
  102.         /*
  103.          * Only for settings files! **
  104.          * Isset, SetValue, GetValue, DeleteValue and GetArray
  105.          */
  106.         public static bool IsSettigs(string path)
  107.         {
  108.             if (!System.IO.File.Exists(path))
  109.                 return false;
  110.  
  111.             return Isset(path, "path");
  112.         }
  113.         public static bool Isset(string path, string key)
  114.         {
  115.             if (System.IO.File.Exists(path))
  116.             {
  117.                 string[] lines = System.IO.File.ReadAllLines(path);
  118.  
  119.                 foreach (string line in lines)
  120.                 {
  121.                     string[] matches = line.Split('=');
  122.                     if (matches.Length <= 0) return false;
  123.                     if (matches[0] == key) return true;
  124.                 }
  125.  
  126.                 return false;
  127.             }
  128.             return false;
  129.         }
  130.         public static bool SetValue(string path, string key, string value)
  131.         {
  132.             if (!System.IO.File.Exists(path))
  133.                 return false;
  134.  
  135.             if (Isset(path, key))
  136.             {
  137.                 string[] lines = System.IO.File.ReadAllLines(path);
  138.                 System.IO.File.WriteAllText(path, null);
  139.  
  140.                 System.IO.StreamWriter file = System.IO.File.AppendText(path);
  141.                 foreach (string line in lines)
  142.                 {
  143.                     string[] matches = line.Split('=');
  144.                     if (matches.Length <= 0) return false;
  145.                     if (matches[0] == key)
  146.                     {
  147.                         file.WriteLine(key + (value.Length <= 0 ? null : "=") + value.ToString());
  148.                     } else
  149.                     {
  150.                         file.WriteLine(line);
  151.                     }
  152.                 }
  153.                 file.Close();
  154.             }
  155.             else
  156.             {
  157.                 System.IO.StreamWriter file = System.IO.File.AppendText(path);
  158.                 file.WriteLine(key + (value.Length <= 0 ? null : "=") + value.ToString());
  159.                 file.Close();
  160.             }
  161.  
  162.             return true;
  163.         }
  164.         public static bool DeleteValue(string path, string key)
  165.         {
  166.             if (!System.IO.File.Exists(path) || !Isset(path, key))
  167.                 return false;
  168.  
  169.             string[] lines = System.IO.File.ReadAllLines(path);
  170.             System.IO.File.WriteAllText(path, null);
  171.  
  172.             System.IO.StreamWriter file = System.IO.File.AppendText(path);
  173.             foreach (string line in lines)
  174.             {
  175.                 string[] matches = line.Split('=');
  176.                 if (matches.Length <= 0) return false;
  177.                 if (matches[0] != key)
  178.                 {
  179.                     file.WriteLine(line);
  180.                 }
  181.             }
  182.             file.Close();
  183.  
  184.             return true;
  185.         }
  186.         public static bool ResetValue(string path, string key)
  187.         {
  188.             if (!System.IO.File.Exists(path) || !Isset(path, key))
  189.                 return false;
  190.  
  191.             return SetValue(path, key, "");
  192.         }
  193.         public static string GetValue(string path, string key)
  194.         {
  195.             if (!System.IO.File.Exists(path) || !Isset(path, key))
  196.                 return "none";
  197.  
  198.             string[] lines = System.IO.File.ReadAllLines(path);
  199.  
  200.             foreach (string line in lines)
  201.             {
  202.                 string[] matches = line.Split('=');
  203.                 if (matches.Length <= 1) return "none";
  204.                 if (matches[0] == key) return matches[1].ToString();
  205.             }
  206.  
  207.             return "none";
  208.         }
  209.         public static string[] GetArray(string path, string separator = "=")
  210.         {
  211.             if (!System.IO.File.Exists(path))
  212.                 return new string[0];
  213.  
  214.             if (separator.Length <= 0) separator = "=";
  215.             if (separator == "=")
  216.             {
  217.                 string[] lines = System.IO.File.ReadAllLines(path);
  218.                 return lines;
  219.             }
  220.             else
  221.             {
  222.                 string[] lines = System.IO.File.ReadAllLines(path);
  223.                 int counter = 0;
  224.  
  225.                 foreach (string line in lines)
  226.                 {
  227.                     string[] matches = line.Split('=');
  228.                     if (matches.Length <= 1)
  229.                     {
  230.                         lines[counter] = line;
  231.                         goto nextline;
  232.                     }
  233.  
  234.                     if (matches.Length > 2)
  235.                     {
  236.                         string addings = null;
  237.                         for(int i = 1; i <= matches.Length - 1; i++)
  238.                         {
  239.                             addings = addings + (i>1? ("="):("")) + matches[i];
  240.                         }
  241.                         lines[counter] = matches[0] + separator + addings;
  242.                     }
  243.                     else
  244.                     {
  245.                         lines[counter] = matches[0] + separator + matches[1];
  246.                     }
  247.                    
  248.                 nextline:
  249.                     counter++;
  250.                 }
  251.                 return lines;
  252.             }
  253.         }
  254.  
  255.         /*
  256.          * Conventer - Get
  257.          */
  258.         public static int GetInt32(string path, string key)
  259.         {
  260.             try
  261.             {
  262.                 return Convert.ToInt32(GetValue(path, key));
  263.             } catch(Exception)
  264.             {
  265.                 return (int) 0;
  266.             }
  267.         }
  268.         public static long GetInt64(string path, string key)
  269.         {
  270.             try
  271.             {
  272.                 return Convert.ToInt64(GetValue(path, key));
  273.             }
  274.             catch (Exception)
  275.             {
  276.                 return (long) 0;
  277.             }
  278.         }
  279.         public static bool GetBool(string path, string key)
  280.         {
  281.             try
  282.             {
  283.                 return Convert.ToBoolean(GetValue(path, key));
  284.             } catch(Exception)
  285.             {
  286.                 return (bool) false;
  287.             }
  288.            
  289.         }
  290.     }
  291. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement