Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Text.RegularExpressions;
- /*
- * =========================================================================================================
- * = Writed By RaFaeL ZIlberman, basic for file system. =
- * = This script is have some bugs and writed just for learning =
- * =========================================================================================================
- */
- namespace Files
- {
- class Files
- {
- /*
- * Create files
- */
- public static bool Create(string path)
- {
- if (System.IO.File.Exists(path))
- return false;
- System.IO.FileStream file = System.IO.File.Create(path);
- file.Close();
- SetValue(path, "path", path);
- return true;
- }
- /*
- * Empty files
- */
- public static bool Empty(string path)
- {
- if (!System.IO.File.Exists(path))
- return false;
- System.IO.File.WriteAllText(path, null);
- return true;
- }
- /*
- * Copy and Move files
- */
- public static bool Copy(string path, string newpath, bool destroyfile = true, bool applayfile = false)
- {
- if (!System.IO.File.Exists(path) || (destroyfile == false && System.IO.File.Exists(newpath)))
- return false;
- if (applayfile == true)
- {
- System.IO.StreamWriter newfile = System.IO.File.AppendText(newpath);
- string file = System.IO.File.ReadAllText(path);
- newfile.Write("\r\n" + file);
- newfile.Close();
- }
- else
- {
- System.IO.File.Copy(path, newpath);
- }
- return true;
- }
- public static bool Move(string path, string newpath, bool destroyfile = false, bool applayfile = false)
- {
- if (!System.IO.File.Exists(path) || (destroyfile == false && System.IO.File.Exists(newpath)))
- return false;
- if (applayfile == true)
- {
- System.IO.StreamWriter newfile = System.IO.File.AppendText(newpath);
- string file = System.IO.File.ReadAllText(path);
- newfile.Write("\r\n" + file);
- newfile.Close();
- System.IO.File.Delete(path);
- }
- else
- {
- System.IO.File.Move(path, newpath);
- }
- return true;
- }
- /*
- * Convet file to setting's file
- */
- public static bool AddPath(string path, string main = null)
- {
- if (!System.IO.File.Exists(path))
- return false;
- return SetValue(path, "path", path);
- }
- /*
- * Only for settings files! **
- * Isset, SetValue, GetValue, DeleteValue and GetArray
- */
- public static bool IsSettigs(string path)
- {
- if (!System.IO.File.Exists(path))
- return false;
- return Isset(path, "path");
- }
- public static bool Isset(string path, string key)
- {
- if (System.IO.File.Exists(path))
- {
- string[] lines = System.IO.File.ReadAllLines(path);
- foreach (string line in lines)
- {
- string[] matches = line.Split('=');
- if (matches.Length <= 0) return false;
- if (matches[0] == key) return true;
- }
- return false;
- }
- return false;
- }
- public static bool SetValue(string path, string key, string value)
- {
- if (!System.IO.File.Exists(path))
- return false;
- if (Isset(path, key))
- {
- string[] lines = System.IO.File.ReadAllLines(path);
- System.IO.File.WriteAllText(path, null);
- System.IO.StreamWriter file = System.IO.File.AppendText(path);
- foreach (string line in lines)
- {
- string[] matches = line.Split('=');
- if (matches.Length <= 0) return false;
- if (matches[0] == key)
- {
- file.WriteLine(key + (value.Length <= 0 ? null : "=") + value.ToString());
- } else
- {
- file.WriteLine(line);
- }
- }
- file.Close();
- }
- else
- {
- System.IO.StreamWriter file = System.IO.File.AppendText(path);
- file.WriteLine(key + (value.Length <= 0 ? null : "=") + value.ToString());
- file.Close();
- }
- return true;
- }
- public static bool DeleteValue(string path, string key)
- {
- if (!System.IO.File.Exists(path) || !Isset(path, key))
- return false;
- string[] lines = System.IO.File.ReadAllLines(path);
- System.IO.File.WriteAllText(path, null);
- System.IO.StreamWriter file = System.IO.File.AppendText(path);
- foreach (string line in lines)
- {
- string[] matches = line.Split('=');
- if (matches.Length <= 0) return false;
- if (matches[0] != key)
- {
- file.WriteLine(line);
- }
- }
- file.Close();
- return true;
- }
- public static bool ResetValue(string path, string key)
- {
- if (!System.IO.File.Exists(path) || !Isset(path, key))
- return false;
- return SetValue(path, key, "");
- }
- public static string GetValue(string path, string key)
- {
- if (!System.IO.File.Exists(path) || !Isset(path, key))
- return "none";
- string[] lines = System.IO.File.ReadAllLines(path);
- foreach (string line in lines)
- {
- string[] matches = line.Split('=');
- if (matches.Length <= 1) return "none";
- if (matches[0] == key) return matches[1].ToString();
- }
- return "none";
- }
- public static string[] GetArray(string path, string separator = "=")
- {
- if (!System.IO.File.Exists(path))
- return new string[0];
- if (separator.Length <= 0) separator = "=";
- if (separator == "=")
- {
- string[] lines = System.IO.File.ReadAllLines(path);
- return lines;
- }
- else
- {
- string[] lines = System.IO.File.ReadAllLines(path);
- int counter = 0;
- foreach (string line in lines)
- {
- string[] matches = line.Split('=');
- if (matches.Length <= 1)
- {
- lines[counter] = line;
- goto nextline;
- }
- if (matches.Length > 2)
- {
- string addings = null;
- for(int i = 1; i <= matches.Length - 1; i++)
- {
- addings = addings + (i>1? ("="):("")) + matches[i];
- }
- lines[counter] = matches[0] + separator + addings;
- }
- else
- {
- lines[counter] = matches[0] + separator + matches[1];
- }
- nextline:
- counter++;
- }
- return lines;
- }
- }
- /*
- * Conventer - Get
- */
- public static int GetInt32(string path, string key)
- {
- try
- {
- return Convert.ToInt32(GetValue(path, key));
- } catch(Exception)
- {
- return (int) 0;
- }
- }
- public static long GetInt64(string path, string key)
- {
- try
- {
- return Convert.ToInt64(GetValue(path, key));
- }
- catch (Exception)
- {
- return (long) 0;
- }
- }
- public static bool GetBool(string path, string key)
- {
- try
- {
- return Convert.ToBoolean(GetValue(path, key));
- } catch(Exception)
- {
- return (bool) false;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement