C# .ini Dosyası Okuma Sınıfı

X0r Çevrimdışı

X0r

Usta Üye
Yönetici
Katılım
29 May 2024
Mesajlar
80
Tepkime puanı
1
C#:
  internal class Program
  {
      static void Main(string[] args)
      {
          IniFile iniFile = new IniFile("ayarlar.ini");
          string ayarlar = "ayarlar";
          string baslik = iniFile.Read("baslik", ayarlar);
          string dosya = iniFile.Read("dosya", ayarlar);
          string hedef = iniFile.Read("hedef", ayarlar);

          Console.Title = baslik;
          Console.ForegroundColor = ConsoleColor.Green; // Yeşil renk

      Console.WriteLine("██╗  ██╗ ██████╗ ██████╗ ████████╗ ██████╗");
      Console.WriteLine("╚██╗██╔╝██╔═████╗██╔══██╗╚══██╔══╝██╔════╝");
      Console.WriteLine(" ╚███╔╝ ██║██╔██║██████╔╝   ██║   ██║");
      Console.WriteLine(" ██╔██╗ ████╔╝██║██╔══██╗   ██║   ██║");  
      Console.WriteLine("██╔╝ ██╗╚██████╔╝██║  ██║██╗██║   ╚██████╗");
      Console.WriteLine("╚═╝  ╚═╝ ╚═════╝ ╚═╝  ╚═╝╚═╝╚═╝    ╚═════╝");
          Console.ResetColor();
          Console.WriteLine();
          Console.WriteLine(baslik + "Yükleniyor");


          // Uygulamanın çalıştığı dizini al
          string currentDirectory = AppDomain.CurrentDomain.BaseDirectory;

          // Kopyalanacak dosyanın adı
          string fileName = dosya;

          // Kopyalanacak dosyanın tam yolu
          string sourceFilePath = Path.Combine(currentDirectory, fileName);

          // Kullanıcının Belgelerim klasörünü al
          string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

          // Hedef klasörün yolu
          string targetFolder = Path.Combine(documentsPath, hedef);

          // Klasör yoksa oluştur
          if (!Directory.Exists(targetFolder))
          {
              Directory.CreateDirectory(targetFolder);
          }

          // Hedef dosyanın tam yolu
          string destinationFilePath = Path.Combine(targetFolder, fileName);

          // Dosyayı kopyala
          try
          {
              File.Copy(sourceFilePath, destinationFilePath, true);
              Console.WriteLine($"Dosya başarıyla kopyalandı: {destinationFilePath}");
          }
          catch (Exception ex)
          {
              Console.WriteLine($"Dosya kopyalanırken bir hata oluştu: {ex.Message}");
          }
      }
  }


IniFile.cs Dosyası içeriği

C#:
 internal class IniFile
 {
     string Path;
     string EXE = Assembly.GetExecutingAssembly().GetName().Name;

     [DllImport("kernel32", CharSet = CharSet.Unicode)]
     static extern long WritePrivateProfileString(string Section, string Key, string Value, string FilePath);

     [DllImport("kernel32", CharSet = CharSet.Unicode)]
     static extern int GetPrivateProfileString(string Section, string Key, string Default, StringBuilder RetVal, int Size, string FilePath);

     public IniFile(string IniPath = null)
     {
         Path = new FileInfo(IniPath ?? EXE + ".ini").FullName;
     }

     public string Read(string Key, string Section = null)
     {
         var RetVal = new StringBuilder(255);
         GetPrivateProfileString(Section ?? EXE, Key, "", RetVal, 255, Path);
         return RetVal.ToString();
     }

     public void Write(string Key, string Value, string Section = null)
     {
         WritePrivateProfileString(Section ?? EXE, Key, Value, Path);
     }

     public void DeleteKey(string Key, string Section = null)
     {
         Write(Key, null, Section ?? EXE);
     }

     public void DeleteSection(string Section = null)
     {
         Write(null, null, Section ?? EXE);
     }

     public bool KeyExists(string Key, string Section = null)
     {
         return Read(Key, Section).Length > 0;
     }
 }
 
Geri
Üst