Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.90 KB | None | 0 0
  1. using Forms1;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.ComponentModel;
  5. using System.Data;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Threading.Tasks;
  11. using System.Windows.Forms;
  12.  
  13. namespace WindowsFormsApp2
  14. {
  15. public partial class Form1 : Form
  16. {
  17.  
  18. string drive;
  19. public string file;
  20.  
  21. public Form1()
  22. {
  23. InitializeComponent();
  24. }
  25.  
  26. private void getDriveInfoButton_Click(object sender, EventArgs e)
  27. {
  28. drivesListBox.Items.Clear();
  29. drivesListBox.Items.Add("====Drives Info====");
  30. DriveInfo[] drives = DriveInfo.GetDrives();
  31. foreach(DriveInfo drive in drives)
  32. {
  33. if (!drive.IsReady) continue;
  34. drivesListBox.Items.Add(drive + "\t" + drive.TotalSize / 1024.0f / 1024 / 1024 + "GB");
  35. }
  36. }
  37.  
  38. private void drivesListBox_SelectedIndexChanged(object sender, EventArgs e)
  39. {
  40. if(drivesListBox.SelectedIndex != -1)
  41. {
  42. string temp = drivesListBox.SelectedItem.ToString().Substring(0, 3);
  43. if (Environment.GetLogicalDrives().Contains(temp))
  44. {
  45. drive = temp;
  46. listFolders(new DirectoryInfo(drive));
  47. pathLabel.Text = drive;
  48. }
  49. }
  50. }
  51.  
  52. private void listFolders(DirectoryInfo directoryInfo)
  53. {
  54. foldersListBox.Items.Clear();
  55. DirectoryInfo[] folders = directoryInfo.GetDirectories();
  56. foreach(DirectoryInfo folder in folders)
  57. {
  58. if (folder.Attributes.HasFlag(FileAttributes.Hidden)) continue;
  59. foldersListBox.Items.Add(folder);
  60. }
  61. FileInfo[] files = directoryInfo.GetFiles();
  62. foreach(FileInfo file in files)
  63. {
  64. if (file.Attributes.HasFlag(FileAttributes.Hidden)) continue;
  65. foldersListBox.Items.Add(file);
  66. }
  67. }
  68.  
  69. private void foldersListBox_DoubleClick(object sender, EventArgs e)
  70. {
  71. if(foldersListBox.SelectedIndex != -1)
  72. {
  73. string folder = foldersListBox.SelectedItem.ToString();
  74. if(new DirectoryInfo(pathLabel.Text + folder).Attributes.HasFlag(FileAttributes.Directory))
  75. {
  76. pathLabel.Text = pathLabel.Text + folder + @"\";
  77. listFolders(new DirectoryInfo(pathLabel.Text));
  78. }
  79. else
  80. {
  81. string txt = pathLabel.Text + folder;
  82. if (txt.Contains(".txt"))
  83. {
  84. Form2 textEditor = new Form2();
  85. textEditor.Show();
  86. textEditor.newTextFile(txt);
  87. }
  88. else
  89. {
  90. System.Diagnostics.Process.Start(pathLabel.Text + folder);
  91. }
  92. }
  93. }
  94. }
  95.  
  96. private void BackButton_Click(object sender, EventArgs e)
  97. {
  98. if(pathLabel.Text != drive && pathLabel.Text != "N/A")
  99. {
  100. string location = pathLabel.Text.Substring(0, pathLabel.Text.Length - 1);
  101. location = location.Substring(0, location.LastIndexOf(@"\") + 1);
  102. listFolders(new DirectoryInfo(location));
  103. pathLabel.Text = location;
  104. }
  105. }
  106.  
  107. private void newFolderButton_Click(object sender, EventArgs e)
  108. {
  109. if(!string.IsNullOrEmpty(drive) && pathLabel.Text != "N/A")
  110. {
  111. string name = Microsoft.VisualBasic.Interaction.InputBox("Enter folder Name", "Creating new folder", "New Folder");
  112. if (!string.IsNullOrEmpty(name))
  113. {
  114. if(!Directory.Exists(pathLabel.Text + name))
  115. {
  116. try
  117. {
  118. Directory.CreateDirectory(pathLabel.Text + name);
  119. }
  120. catch(UnauthorizedAccessException)
  121. {
  122. MessageBox.Show("Access denied", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  123. return;
  124. }
  125. catch (PathTooLongException)
  126. {
  127. MessageBox.Show("Folder name too long", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  128. return;
  129. }
  130. catch (Exception)
  131. {
  132. MessageBox.Show("FATAL ERROR!", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
  133. return;
  134. }
  135. }
  136. listFolders(new DirectoryInfo(pathLabel.Text));
  137. }
  138. }
  139. }
  140.  
  141. private void deleteButton_Click(object sender, EventArgs e)
  142. {
  143. if(foldersListBox.SelectedIndex != -1)
  144. {
  145. if(MessageBox.Show("Are you sure? You will delete: " + pathLabel.Text + foldersListBox.SelectedItem.ToString(), "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
  146. {
  147. if(new DirectoryInfo(pathLabel.Text + foldersListBox.SelectedItem).Attributes.HasFlag(FileAttributes.Directory))
  148. {
  149. Directory.Delete(pathLabel.Text + foldersListBox.SelectedItem, true);
  150. }
  151. else
  152. {
  153. File.Delete(pathLabel.Text + foldersListBox.SelectedItem);
  154. }
  155. listFolders(new DirectoryInfo(pathLabel.Text));
  156. }
  157. }
  158. }
  159.  
  160. private void newTextFileButton_Click(object sender, EventArgs e)
  161. {
  162. if (String.IsNullOrEmpty(drive) && pathLabel.Text == "N/A") return;
  163. string name = Microsoft.VisualBasic.Interaction.InputBox("Enter Text File Name", "Creating New Text File", "New Text File");
  164. if (String.IsNullOrEmpty(name)) return;
  165. string txt = pathLabel.Text + name;
  166. if (!txt.Contains(".txt")) txt += ".txt";
  167. if (File.Exists(txt)) return;
  168. try
  169. {
  170. File.CreateText(txt).Close();
  171. }
  172. catch (Exception ex)
  173. {
  174. MessageBox.Show(ex.Message);
  175. }
  176. listFolders(new DirectoryInfo(pathLabel.Text));
  177. Form2 myForm = new Form2();
  178. myForm.Show();
  179. myForm.newTextFile(file);
  180. }
  181. }
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement