-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
1689 lines (1590 loc) · 69.6 KB
/
Form1.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Text.RegularExpressions;
namespace computer1
{
public partial class Form1 : Form
{
/*磁盘初始化变量*/
private byte[] fat = new byte[128];
private byte[] root = new byte[64];
private List<string> Names = new List<string>();
public Form1()
{
//format();
InitializeComponent();
//刷新磁盘
init_();
}
/*格式化*/
private void format()
{
string path = "C:/Users/HP/Desktop/expriment/c++_vs/computer1/resource/disk.txt";
FileStream f = new FileStream(path, FileMode.Truncate, FileAccess.ReadWrite);
f.WriteByte((byte)(1));
f.WriteByte((byte)(1));
f.WriteByte((byte)(2));
f.WriteByte((byte)(3));
fat[0] = 1;
fat[1] = 1;
fat[2] = 2;//根目录
fat[3] = 3;//c目录占据
for (int i = 4; i <128; i++)
{
f.WriteByte((byte)(128));
fat[i] = 128;
}
byte[] bytes = Encoding.UTF8.GetBytes("C d");
f.Write(bytes,0,bytes.Length);
f.WriteByte((byte)(3));
f.Write(Encoding.UTF8.GetBytes(" "),0,1);
for (int i = 136; i < 64*128; i++)
{
f.Write(Encoding.UTF8.GetBytes(" "), 0, 1);
}
f.Close();
}
/*刷新磁盘(每改变一次磁盘就刷新一次)*/
private void init_()
{
for(int i=1;i<=128;i++)
{
Label lab = (this.Controls.Find("label" + Convert.ToString(i), true).First()) as Label;
lab.BackColor = Color.YellowGreen;
}
this.treeView1.Nodes.Clear();
string path = "C:/Users/HP/Desktop/expriment/c++_vs/computer1/resource/disk.txt";
FileStream f = new FileStream(path, FileMode.Open, FileAccess.Read);
f.Read(fat, 0, 128);
f.Read(root, 0, 64);
for (int i = 0; i < 128; i++)
{
if (fat[i] != (byte)(128))
{
Label lab = (this.Controls.Find("label" + Convert.ToString(i + 1), true).First()) as Label;
lab.BackColor = Color.DodgerBlue;
}
}
f.Close();
//初始化根目录
string filename = "";
int start = 0;
for (int i = 0; i < 64; i = i + 8)
{
if (root[i] != 32)
{
byte[] item = new byte[8];
for (int j = 0; j < 8; j++)
{
item[j] = root[i + j];
}
filename = Encoding.UTF8.GetString(item,0,3).Trim();
Names.Add(filename);
start = item[6];
TreeNode newnode = new TreeNode(filename, 2, 3);
newnode.Name = filename;
file_option.Traverse(start, newnode,menu2,menu3,menu3e,Names);
this.treeView1.Nodes.Add(newnode);
newnode.ContextMenuStrip = menu2;
newnode.Expand();
}
}
treeView1.TreeViewNodeSorter = new file_option();
}
/*menu1右键菜单*/
private void 添加根目录ToolStripMenuItem_Click(object sender, EventArgs e)
{
string path = "C:/Users/HP/Desktop/expriment/c++_vs/computer1/resource/disk.txt";
//判断根目录是否已满
int count = this.treeView1.GetNodeCount(false);
if(count>=8)
{
MessageBox.Show("根目录已满!");
return;
}
//视图中创建树
edit_file window = new edit_file();
window.Enable2 = false;
window.Enable3 = false;
window.ShowDialog();
string file_name=window.file_name;
if (file_name == "")
return;
else if(Names.Contains(file_name))
{
MessageBox.Show("该名称已存在!");
return;
}
Names.Add(file_name);
//磁盘中写入文件控制块
//寻找磁盘块
int i = 3;
for(;i<128;i++)
{
if (fat[i] == 128)
break;
else if (i == 127)
{
MessageBox.Show("磁盘空间已满!");
return;
}
}
//改变文件控制块,改变位示图
fat[i] = (byte)(i);
Label lab = (this.Controls.Find("label" + Convert.ToString(i + 1), true).First()) as Label;
lab.BackColor = Color.DodgerBlue;
//写入控制块
file_option.add_tree(path, file_name,2, i,count);//2是根目录目录项存放处,i是其实盘块号,count是目录项数量
//写入图标
TreeNode newnode = new TreeNode(file_name, 2, 3);
newnode.Name = file_name;
newnode.ContextMenuStrip = menu2;
this.treeView1.Nodes.Add(newnode);
}
private void 格式化磁盘ToolStripMenuItem_Click(object sender, EventArgs e)
{
format();
init_();
}
/*menu2右键菜单*/
private void 添加目录ToolStripMenuItem_Click(object sender, EventArgs e)
{
//判断子节点的数量是否超过限制
int count = this.treeView1.SelectedNode.GetNodeCount(false);
if(count==8)
{
MessageBox.Show("目录数量达到上限!");
return;
}
//视图中创建树
edit_file window = new edit_file();
window.Enable2 = false;
window.Enable3 = false;
window.ShowDialog();
string file_name = window.file_name;
if (file_name == "")
return;
else if (Names.Contains(file_name))
{
MessageBox.Show("该名称已存在!");
return;
}
Names.Add(file_name);
//查找父节点的起始盘块号
string path = "C:/Users/HP/Desktop/expriment/c++_vs/computer1/resource/disk.txt";
string name_parent = this.treeView1.SelectedNode.Text;
string name = "";
FileStream f = new FileStream(path, FileMode.Open, FileAccess.Read);
f.Seek(128, SeekOrigin.Begin);
int j = 128;
int store;
while(true)
{
byte[] bytes = new byte[3];
byte[] bytes1 = new byte[1];
int i;
for(i=0;i<3;i++)
{
f.Read(bytes1, 0, 1);
if (bytes1[0]==Encoding.UTF8.GetBytes(" ").First())
{
break;
}
bytes[i] = bytes1[0];
}
name = Encoding.UTF8.GetString(bytes,0,i);
if (name==name_parent)
{
f.Seek(j + 6, SeekOrigin.Begin);
store=f.ReadByte();
break;
}
else
{
j = j + 8;
f.Seek(j, SeekOrigin.Begin);
}
}
f.Close();
//查找空闲块
int start = 3;
for (; start < 128; start++)
{
if (fat[start] == 128)
break;
else if (start == 127)
{
MessageBox.Show("磁盘空间已满!");
return;
}
}
//刷新位示图
fat[start] = (byte)(start);
Label lab = (this.Controls.Find("label" + Convert.ToString(start + 1), true).First()) as Label;
lab.BackColor = Color.DodgerBlue;
//向父目录节点内写入文件控制块信息
file_option.add_tree(path, file_name, store, start, count);
//写入图标
TreeNode newnode = new TreeNode(file_name, 2, 3);
newnode.Name = file_name;
newnode.ContextMenuStrip = menu2;
this.treeView1.SelectedNode.Nodes.Add(newnode);
this.treeView1.SelectedNode.Expand();
treeView1.TreeViewNodeSorter = new file_option();
}
private void 添加文件ToolStripMenuItem_Click(object sender, EventArgs e)
{
/*判断子节点的数量是否超过限制*/
int count = this.treeView1.SelectedNode.GetNodeCount(false);
if (count == 8)
{
MessageBox.Show("文件数量达到上限!");
return;
}
/*视图中创建树*/
edit_file window = new edit_file();
window.Enable3 = true;
window.ShowDialog();
if (window.DialogResult == DialogResult.Cancel)
return;
string file_name = window.file_name;
string file_type = window.file_type;
string context = window.context;
if (context.Length>256)
{
MessageBox.Show("字数超出限制!");
return;
}
if (Names.Contains(file_name))
{
MessageBox.Show("该名称已存在!");
return;
}
Names.Add(file_name);
/*查找父节点的起始盘块号*/
string path = "C:/Users/HP/Desktop/expriment/c++_vs/computer1/resource/disk.txt";
string name_parent = this.treeView1.SelectedNode.Text;
string name = "";
FileStream f = new FileStream(path, FileMode.Open, FileAccess.Read);
f.Seek(128, SeekOrigin.Begin);
int j = 128;
int store;//起始盘块号
while (true)
{
byte[] bytes = new byte[3];
byte[] bytes1 = new byte[1];
int i;
for (i = 0; i < 3; i++)
{
f.Read(bytes1, 0, 1);
if (bytes1[0] == Encoding.UTF8.GetBytes(" ").First())
{
break;
}
bytes[i] = bytes1[0];
}
name = Encoding.UTF8.GetString(bytes, 0, i);
if (name == name_parent)
{
f.Seek(j + 6, SeekOrigin.Begin);
store = f.ReadByte();
break;
}
else
{
j = j + 8;
f.Seek(j, SeekOrigin.Begin);
}
}
f.Close();
/*查找空闲块并刷新位示图*/
int start = 4;
int count_;
int pre=0;
int start_num = 0 ;
if (context.Length % 64 == 0)
count_ = context.Length / 64;
else
count_ = context.Length / 64+1;
for (; start < 128; start++)
{
if (fat[start] == 128)
{
pre = start;
start_num = start;
fat[pre] = (byte)(start);
Label lab = (Controls.Find("label" + Convert.ToString(start + 1), true).First()) as Label;
lab.BackColor = Color.DodgerBlue;
count_--;
start++;
break;
}
else if(start==127)
{
MessageBox.Show("磁盘空间已满!");
return;
}
}
while (count_ > 0)
{
for (; start < 128; start++)
{
if(fat[start] == 128)
{
if (count_ <= 0)
break;
fat[pre] = (byte)(start);
fat[start] = (byte)(start);
pre = start;
count_--;
//刷新位示图
Label lab= (this.Controls.Find("label" + Convert.ToString(start + 1), true).First()) as Label;
lab.BackColor = Color.DodgerBlue;
}
else if (start == 127)
{
MessageBox.Show("磁盘空间已满!");
return;
}
}
}
//向父目录节点内写入文件控制块信息
file_option.add_file(path, file_name,file_type,store, start_num,fat,context,count);
//写入图标
TreeNode newnode = new TreeNode(file_name, 0, 1);
if(file_type=="ex")
newnode.ContextMenuStrip = menu3e;
else
newnode.ContextMenuStrip = menu3;
newnode.Name = file_name;
this.treeView1.SelectedNode.Nodes.Add(newnode);
this.treeView1.SelectedNode.Expand();
treeView1.TreeViewNodeSorter = new file_option();
}
private void 删除目录ToolStripMenuItem_Click(object sender, EventArgs e)
{
/*查找该节点的起始盘块号*/
string path = "C:/Users/HP/Desktop/expriment/c++_vs/computer1/resource/disk.txt";
string filename = this.treeView1.SelectedNode.Text;//文件名
string filetype;//文件类型
string name = "";
FileStream f = new FileStream(path, FileMode.Open, FileAccess.Read);
f.Seek(128, SeekOrigin.Begin);
int j = 128;
int store;//起始盘块号
while (true)
{
byte[] bytes = new byte[3];
byte[] bytes1 = new byte[1];
byte[] bytes2 = new byte[2];
int i;
for (i = 0; i < 3; i++)
{
f.Read(bytes1, 0, 1);
if (bytes1[0] == Encoding.UTF8.GetBytes(" ").First())
{
break;
}
bytes[i] = bytes1[0];
}
name = Encoding.UTF8.GetString(bytes, 0, i);
if (name == filename)
{
f.Seek(j + 3, SeekOrigin.Begin);
f.Read(bytes2, 0, 2);
filetype = Encoding.UTF8.GetString(bytes2).Trim();
f.Seek(j + 6, SeekOrigin.Begin);
store = f.ReadByte();
break;
}
else
{
j = j + 8;
f.Seek(j, SeekOrigin.Begin);
}
}
f.Close();
/*递归删除目录项*/
byte[] disk = new byte[128 * 64];//读磁盘
f = new FileStream(path, FileMode.Open, FileAccess.Read);
f.Read(disk, 0, 128 * 64);
f.Close();
Stack<string> fname = new Stack<string>();//存文件名称
Stack<string> ftype = new Stack<string>();//存文件类型
Stack<int> fstore = new Stack<int>();//存文件起始盘块号
byte[] file_context = new byte[64];
fname.Push(filename);
ftype.Push("d");
fstore.Push(store);
while(fstore.Count!=0)
{
filename = fname.Pop();
Names.Remove(filename);
store = fstore.Pop();
filetype = ftype.Pop();
for(int i=store*64;i<(store+1)*64;i++)
{
file_context[i - store * 64] = disk[i];
}
if(filetype=="d")//如果是目录的话
{
disk[store] = 128;//更改文件分配表
for (int i = store * 64; i < (store + 1) * 64; i++)//删除内容
{
disk[i] = 32;
}
for (int jj=0;jj<64;jj=jj+8)
{
if (file_context[jj] != 32)
{
filename= Encoding.UTF8.GetString(file_context, jj, 3).Trim();
filetype = Encoding.UTF8.GetString(file_context, jj + 5, 1);
store = file_context[jj + 6];
fname.Push(filename);
ftype.Push(filetype);
fstore.Push(store);
}
else
break;
}
}
else if(filetype=="l")//如果是文件的话
{
while(disk[store]!=store)
{
//清理空间
for (int i = store * 64; i < (store + 1) * 64; i++)
{
disk[i] = 32;
}
//更改文件分配表
int t = disk[store];
disk[store] = 128;
store = t;
}
if(disk[store]==store)
{
//清理空间
for (int i = store * 64; i < (store + 1) * 64; i++)
{
disk[i] = 32;
}
//更改文件分配表
disk[store] = 128;
}
}
}
for (int i = j; i < ((j / 64 + 1) * 64 - 8); i++)
{
disk[i] = disk[i + 8];
}
for (int i = ((j / 64 + 1) * 64 - 8); i < (j / 64 + 1) * 64; i++)
{
disk[i] = 32;
}
f = new FileStream(path, FileMode.Truncate, FileAccess.Write);
f.Write(disk, 0, 128 * 64);
f.Close();
treeView1.SelectedNode.Nodes.Clear();
treeView1.Nodes.Remove(treeView1.SelectedNode);
init_();
}
/*menu3右键菜单*/
private void 编辑文件ToolStripMenuItem_Click(object sender, EventArgs e)
{
/*查找该节点的起始盘块号*/
string path = "C:/Users/HP/Desktop/expriment/c++_vs/computer1/resource/disk.txt";
string filename = this.treeView1.SelectedNode.Text;//文件名
string filetype;//文件类型
int count = this.treeView1.SelectedNode.Parent.GetNodeCount(false);
string name = "";
FileStream f = new FileStream(path, FileMode.Open, FileAccess.Read);
f.Seek(128, SeekOrigin.Begin);
int j = 128;
int store;//起始盘块号
while (true)
{
byte[] bytes = new byte[3];
byte[] bytes1 = new byte[1];
byte[] bytes2 = new byte[2];
int i;
for (i = 0; i < 3; i++)
{
f.Read(bytes1, 0, 1);
if (bytes1[0] == Encoding.UTF8.GetBytes(" ").First())
{
break;
}
bytes[i] = bytes1[0];
}
name = Encoding.UTF8.GetString(bytes, 0, i);
if (name == filename)
{
f.Seek(j+3, SeekOrigin.Begin);
f.Read(bytes2, 0, 2);
filetype = Encoding.UTF8.GetString(bytes2).Trim();
f.Seek(j + 6, SeekOrigin.Begin);
store = f.ReadByte();
break;
}
else
{
j = j + 8;
f.Seek(j, SeekOrigin.Begin);
}
}
f.Close();
/*提取内容*/
string context="";
int ii = store;
while(fat[ii]!=ii)
{
int next = fat[store];
f = new FileStream(path, FileMode.Open, FileAccess.Read);
f.Seek(store * 64, SeekOrigin.Begin);
byte[] t = new byte[64];
f.Read(t, 0, 64);
f.Close();
context = context+Encoding.UTF8.GetString(t);
ii = fat[ii];
}
f = new FileStream(path, FileMode.Open, FileAccess.Read);
f.Seek(ii*64, SeekOrigin.Begin);
byte[] bytes3 = new byte[64];
f.Read(bytes3, 0, 64);
f.Close();
context = context+Encoding.UTF8.GetString(bytes3,0,64).Trim();
/*修改文件内容*/
edit_file window = new edit_file();
string k = filename;//用于Names
window.context = context;
window.file_name = filename;
window.file_type = filetype;
window.ShowDialog();
if (window.DialogResult == DialogResult.Cancel)
return;
filename = window.file_name;
filetype = window.file_type;
context = window.context;
if (context.Length > 256)
{
MessageBox.Show("字数超出限制!");
return;
}
if (!Names.Contains(filename))
{
Names.Remove(k);
Names.Add(filename);
this.treeView1.SelectedNode.Text = filename;//修改节点名字
this.treeView1.SelectedNode.Name = filename;
}
if (filetype == "ex")
this.treeView1.SelectedNode.ContextMenuStrip = menu3e;
//更改位示图
ii = store;
while(fat[ii]!=ii)
{
Label lab1 = (this.Controls.Find("label" + Convert.ToString(ii + 1), true).First()) as Label;
lab1.BackColor = Color.YellowGreen;
int t = fat[ii];
fat[ii] = 128;
ii = t;
}
if (fat[ii] == ii)
{
Label lab1 = (this.Controls.Find("label" + Convert.ToString(ii + 1), true).First()) as Label;
lab1.BackColor = Color.YellowGreen;
fat[ii] = 128;
}
//根据新的内容重新查找空闲块
int start = 4;
int count_;
int pre = 0;
int start_num = 0;
if (context.Length % 64 == 0)
count_ = context.Length / 64;
else
count_ = context.Length / 64 + 1;
for (; start < 128; start++)
{
if (fat[start] == 128)
{
pre = start;
start_num = start;
fat[pre] = (byte)(start);
Label lab1 = (this.Controls.Find("label" + Convert.ToString(start + 1), true).First()) as Label;
lab1.BackColor = Color.DodgerBlue;
count_--;
start++;
break;
}
else if (start == 127)
{
MessageBox.Show("磁盘空间已满!");
return;
}
}
while (count_ > 0)
{
for (; start < 128; start++)
{
if (fat[start] == 128)
{
if (count_ <= 0)
break;
fat[pre] = (byte)(start);
fat[start] = (byte)(start);
pre = start;
count_--;
//刷新位示图
Label lab1 = (this.Controls.Find("label" + Convert.ToString(start + 1), true).First()) as Label;
lab1.BackColor = Color.DodgerBlue;
}
else if (start == 127)
{
MessageBox.Show("磁盘空间已满!");
return;
}
}
}
//重新写入内容
file_option.add_file(path, filename, filetype, j/64, start_num, fat, context, count - 1);
}
private void 删除文件ToolStripMenuItem_Click(object sender, EventArgs e)
{
/*查找该节点的起始盘块号*/
string path = "C:/Users/HP/Desktop/expriment/c++_vs/computer1/resource/disk.txt";
string filename=this.treeView1.SelectedNode.Text;//文件名
string name = "";
FileStream f = new FileStream(path, FileMode.Open, FileAccess.Read);
f.Seek(128, SeekOrigin.Begin);
int j = 128;
int store;//起始盘块号
while (true)
{
byte[] bytes = new byte[3];
byte[] bytes1 = new byte[1];
byte[] bytes2 = new byte[2];
f.Read(bytes, 0, 3);
name = Encoding.UTF8.GetString(bytes, 0, 3).Trim();
if (name == filename)
{
f.Seek(j + 3, SeekOrigin.Begin);
f.Read(bytes2, 0, 2);
f.Seek(j + 6, SeekOrigin.Begin);
store = f.ReadByte();
break;
}
else
{
j = j + 8;
f.Seek(j, SeekOrigin.Begin);
}
}
f.Close();
//修改文件分配表 修改位示图
int ii = store;
Stack<int> p=new Stack<int>();
while(fat[ii]!=ii)
{
p.Push(ii);
Label lab1 = (this.Controls.Find("label" + Convert.ToString(ii + 1), true).First()) as Label;
lab1.BackColor = Color.YellowGreen;
int t = fat[ii];
fat[ii] = 128;
ii = t;
}
if (fat[ii] == ii)
{
p.Push(ii);
Label lab1 = (this.Controls.Find("label" + Convert.ToString(ii + 1), true).First()) as Label;
lab1.BackColor = Color.YellowGreen;
fat[ii] = 128;
}
//写入磁盘
f = new FileStream(path, FileMode.Open);
byte[] disk = new byte[128 * 64];
f.Read(disk, 0, 128 * 64);
f.Close();
//文件分配表
for (int i = 0; i < 128; i++)
{
disk[i] = fat[i];
}
//文件内容
while(p.Count!=0)
{
int m = p.Pop();
for(int i=0;i<64;i++)
{
disk[m * 64 + i] = 32;
}
}
//文件控制块
for (int i = j; i < ((j/64+1)*64-8); i++)
{
disk[i] = disk[i + 8];
}
for(int i= ((j / 64 + 1) * 64-8); i< (j / 64 + 1) * 64;i++)
{
disk[i] = 32;
}
f = new FileStream(path, FileMode.Truncate, FileAccess.Write);
f.Write(disk, 0, disk.Length);
f.Close();
//修改视图
treeView1.Nodes.Remove(treeView1.SelectedNode);
Names.Remove(filename);
}
/*menu3e右键菜单*/
private void 运行文件ToolStripMenuItem_Click(object sender, EventArgs e)
{
/*查找该节点的起始盘块号*/
string path = "C:/Users/HP/Desktop/expriment/c++_vs/computer1/resource/disk.txt";
string filename = this.treeView1.SelectedNode.Text;//文件名
string filetype;//文件类型
int count = this.treeView1.SelectedNode.Parent.GetNodeCount(false);
string name = "";
FileStream f = new FileStream(path, FileMode.Open, FileAccess.Read);
f.Seek(128, SeekOrigin.Begin);
int j = 128;
int store;//起始盘块号
while (true)
{
byte[] bytes = new byte[3];
byte[] bytes1 = new byte[1];
byte[] bytes2 = new byte[2];
int i;
for (i = 0; i < 3; i++)
{
f.Read(bytes1, 0, 1);
if (bytes1[0] == Encoding.UTF8.GetBytes(" ").First())
{
break;
}
bytes[i] = bytes1[0];
}
name = Encoding.UTF8.GetString(bytes, 0, i);
if (name == filename)
{
f.Seek(j + 3, SeekOrigin.Begin);
f.Read(bytes2, 0, 2);
filetype = Encoding.UTF8.GetString(bytes2).Trim();
f.Seek(j + 6, SeekOrigin.Begin);
store = f.ReadByte();
break;
}
else
{
j = j + 8;
f.Seek(j, SeekOrigin.Begin);
}
}
f.Close();
/*提取内容*/
string context = "";
int ii = store;
while (fat[ii] != ii)
{
int next = fat[store];
f = new FileStream(path, FileMode.Open, FileAccess.Read);
f.Seek(store * 64, SeekOrigin.Begin);
byte[] t = new byte[64];
f.Read(t, 0, 64);
f.Close();
context = context + Encoding.UTF8.GetString(t);
ii = fat[ii];
}
f = new FileStream(path, FileMode.Open, FileAccess.Read);
f.Seek(ii * 64, SeekOrigin.Begin);
byte[] bytes3 = new byte[64];
f.Read(bytes3, 0, 64);
f.Close();
context = context + Encoding.UTF8.GetString(bytes3, 0, 64).Trim();
//创建进程控制块插入就绪队列
process.cache = Regex.Split(context, "\r\n");
process.create();
PCB[] r1 = process.Ready.ToArray();
textBox_ready.Text = "";
foreach (PCB i in r1)
{
textBox_ready.Text = textBox_ready.Text + i.id + "\r\n";
}
}
private void 编辑文件ToolStripMenuItem1_Click(object sender, EventArgs e)
{
编辑文件ToolStripMenuItem_Click(sender, e);
}
private void 删除文件ToolStripMenuItem1_Click(object sender, EventArgs e)
{
删除文件ToolStripMenuItem_Click(sender, e);
}
/*命令接口*/
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar==(char)Keys.Enter)
{
string context1 = textBox1.Text;
string[] order = context1.Split(' ');
switch(order[0])
{
case "create": {
//是否重名
if (Names.Contains(order[1]))
{
MessageBox.Show("该名称已存在!");
return;
}
Names.Add(order[1]);
//计算子节点数量
TreeNode tn = treeView1.Nodes.Find(order[3], true).First();
int count = tn.GetNodeCount(false);
if (count == 8)
{
MessageBox.Show("文件数量达到上限!");
return;
}
/*查找父节点的起始盘块号*/
string path = "C:/Users/HP/Desktop/expriment/c++_vs/computer1/resource/disk.txt";
string name = "";
FileStream f = new FileStream(path, FileMode.Open, FileAccess.Read);
f.Seek(128, SeekOrigin.Begin);
int j = 128;
int store;//起始盘块号
while (true)
{
byte[] bytes = new byte[3];
byte[] bytes1 = new byte[1];
int i;
for (i = 0; i < 3; i++)
{
f.Read(bytes1, 0, 1);
if (bytes1[0] == Encoding.UTF8.GetBytes(" ").First())
{
break;
}
bytes[i] = bytes1[0];
}
name = Encoding.UTF8.GetString(bytes, 0, i);
if (name == order[3])
{
f.Seek(j + 6, SeekOrigin.Begin);
store = f.ReadByte();
break;
}
else
{
j = j + 8;
f.Seek(j, SeekOrigin.Begin);
}
}
f.Close();
/*查找空闲块并刷新位示图*/
int start = 4;
int start_num = 0;
for (; start < 128; start++)
{
if (fat[start] == 128)
{
start_num = start;
fat[start_num] = (byte)(start);
Label lab = (this.Controls.Find("label" + Convert.ToString(start + 1), true).First()) as Label;
lab.BackColor = System.Drawing.Color.DodgerBlue;
break;
}
else if (start == 127)
{
MessageBox.Show("磁盘空间已满!");
return;
}
}
//向父目录节点内写入文件控制块信息
file_option.add_file(path, order[1], order[2], store, start_num, fat, "", count);
//写入图标
TreeNode newnode = new TreeNode(order[1], 0, 1);
newnode.Name = order[1];
newnode.ContextMenuStrip = menu3;
tn.Nodes.Add(newnode);
tn.Expand();
treeView1.TreeViewNodeSorter = new file_option();
};break;//文件名 文件类型 父节点名称
case "mkdir": {
//是否重名
if (Names.Contains(order[1]))
{
MessageBox.Show("该名称已存在!");
return;
}
Names.Add(order[1]);
//计算子节点数量
TreeNode tn = treeView1.Nodes.Find(order[2], true).First();
int count = tn.GetNodeCount(false);
if (count == 8)
{
MessageBox.Show("文件数量达到上限!");
return;
}
/*查找父节点的起始盘块号*/
string path = "C:/Users/HP/Desktop/expriment/c++_vs/computer1/resource/disk.txt";
string name = "";
FileStream f = new FileStream(path, FileMode.Open, FileAccess.Read);
f.Seek(128, SeekOrigin.Begin);
int j = 128;
int store;//起始盘块号
while (true)