-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
136 lines (134 loc) · 4.7 KB
/
Copy pathProgram.cs
File metadata and controls
136 lines (134 loc) · 4.7 KB
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
using System;
using System.IO;
namespace document_merger
{
public class merger
{
public string content1,content2,document1,document2,mergedContent,mergedName,ErrorCode;
public merger()
{
document1=getFileName(1);
document2=getFileName(2);
content1=scanfile(document1);
content2=scanfile(document2);
mergedName=rename(document1,document2);
mergedContent+=content1+content2;
ErrorCode=merge(mergedName,mergedContent);
}
public string merge(string mergedName,string mergedContent)
{
try{
StreamWriter sr= new StreamWriter(mergedName);
sr.WriteLine(mergedContent);
sr.Close();
return($"{mergedName} was successfully saved. The document contains {mergedContent.Length} characters");
}
catch(Exception e)
{
return(e.Message);
}
}
public string scanfile(string WD)
{
int len=WD.Length;
if(!(WD[len-1]=='t' && WD[len-2]=='x' &&WD[len-3]=='t'&&WD[len-4]=='.'))
{
WD+=".txt";
}
string content;
StreamReader sr = new StreamReader(WD);
content=sr.ReadToEnd();
sr.Close();
return content;
}
public string getFileName(int tryNumber)
{
string name;
while(true)
{
Console.WriteLine($"Please enter name of ducement{tryNumber}:");
name=Console.ReadLine();
if(name==""||name==".txt"){
Console.WriteLine("File name cannot be blank.");
continue;
}
Boolean bl=isInDirectory(name);
if(bl)
{
int len=name.Length;
break;
}
else
{
continue;
}
}
return name.Replace(".txt","");
}
public string rename(string name1,string name2)
{
while(true){
string name;
Console.WriteLine($"What would you like the string to be called?\nfor default,({name1+name2+".txt"}), input -1:");
name=Console.ReadLine();
if(name==""||name==".txt"){
Console.WriteLine("File name cannot be blank.");
continue;
}
if(name!="-1")
{
int len=name.Length;
if(!(name[len-1]=='t' && name[len-2]=='x' &&name[len-3]=='t'&&name[len-4]=='.')){
name+=".txt";
}
return name;
}
else
{
return(name1+name2+".txt");
}
}
}
public bool isInDirectory(string name)
{
try
{
int len=name.Length;
if(!(name[len-1]=='t' && name[len-2]=='x' &&name[len-3]=='t'&&name[len-4]=='.'))
{
name+=".txt";
}
StreamReader sr = new StreamReader(name);
if(sr==null){
Console.WriteLine("File is empty or missing.");
return false;
}
sr.Close();
return true;
}
catch(FileNotFoundException)
{
Console.WriteLine("Cannot find file.");
return false;
}
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Document Merger\n");
while(true){
merger merge=new merger();
Console.WriteLine(merge.ErrorCode+" Input y to do it again or enter any other character to exit.");
string input=Console.ReadLine();
if(input=="y"){
continue;
}
else{
break;
}
}
}
}
}