c#命令行下的日记本和日程规划(计划任务并删除)(闲来无事写着玩练习c#勿扰喷)
代码如下
using System;
using heart;
namespace cjgf1
{
class Program
{
static void Main(string[] args)
{
int i;
string w1;
Console.Title = "Zing's gf";
Class1 yin1 = new Class1();
yin1.chushi1();
yin1.choice(Console.ReadLine());
yin1.plandui1();
Console.WriteLine("Hello World!");
Console.ReadKey();
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace heart
{
public class Class1
{
public string chushi = "300";
public int love;
public int lonely;
public int sad;
public string path11 = @"G:\\gf1\chushi0.txt";
public string path01 = @"G:\\gf1\hlove.txt";
public string path02 = @"G:\\gf1\hlonely.txt";
public string path03 = @"G:\\gf1\hsad.txt";
public string path04 = @"G:\\gf1\gfno.txt";
public string path05 = @"G:\\gf1\opentimes.txt";
public string path06 = @"G:\\gf1\diary.txt";
public string path07 = @"G:\\gf1\everwords.txt";
public string path08 = @"G:\\gf1\plan.txt";
public string pathverb = @"G:\\gf1\verb.txt";
public string pathadv = @"G:\\gf1\adv.txt";
public string pathadj = @"G:\\gf1\adj.txt";
public string pathn = @"G:\\gf1\n.txt";
public string path09 = @"G:\\gf1\plandui.txt";
//string zhong = "200";
public string str1 = "200";
public int gfno;
public int i;
public string tmp;
public string tmp1;
public void chushi1()
{
str1 = File.ReadAllText(path11, Encoding.Default);
if (chushi != str1)
{
//要写入的文本字符串
string msg = "300";
//在path1指定的路径中创建或覆盖文件,并将msg字符串写入文件中。
FileStream fs = File.Create(path11);//创建文件流,在path1指定的路径中创建或覆盖文件
byte[] contents = Encoding.Default.GetBytes(msg);//将msg字符串的字符编码变成当前ANSI代码页的编码并保存到byte数组中
fs.Write(contents, 0, contents.Length);//从contents中读取字符串,从0位置开始将字符串长度大小的字符串写入到文本中。
fs.Close();//关闭文件流
chushi = "300";
Console.WriteLine("亲爱的!你以后一定要好好爱我哦!");
gfno = stringtoint(File.ReadAllText(path04, Encoding.Default));
gfno = gfno + 1;
writetofile(inttostring(gfno), path04);
Console.WriteLine($"我是你的第{gfno}个女朋友!你一定要爱我超过之前的{gfno-1}个小姐姐!要用心陪女孩子!");
love = 50;
lonely = 0;
sad = 0;
writetofile("300", path11);
writetofile("50", path01);
writetofile("0", path02);
writetofile("0", path03);
writetofile("0", path06);
}
else
{
love = stringtoint(File.ReadAllText(path01, Encoding.Default));
lonely = stringtoint(File.ReadAllText(path02, Encoding.Default));
sad = stringtoint(File.ReadAllText(path03, Encoding.Default));
Console.WriteLine($"第{fileadd(path05)}的相遇,爱你!");
if (love >= 90)
{
Console.WriteLine("好爱你!!!没有你我可怎么办!!!");
}
else
{
if (love >= 60)
{
Console.WriteLine("陪陪我吧,亲爱的");
}
else
{
Console.WriteLine("亲爱的!需要我为你做什么!一定要开心哦!");
}
}
}
}
public int fileadd(string path21)
{
gfno = stringtoint(File.ReadAllText(path21, Encoding.Default));
gfno = gfno + 1;
writetofile(inttostring(gfno), path21);
return gfno;
}
public int filesub(string path22)
{
gfno = stringtoint(File.ReadAllText(path22, Encoding.Default));
gfno = gfno - 1;
writetofile(inttostring(gfno), path22);
return gfno;
}
public int stringtoint(string st11)
{
string s = st11;
int a1 = int.Parse(s);
int a2;
int.TryParse(s, out a2);
int a3 = Convert.ToInt32(s); //这些东西可以吧string字符串转化为int形的数字
return a3;
}
public string inttostring(int in11)
{
int a = in11;
string s1 = a.ToString();
string s2 = Convert.ToString(a);
return s2;
}
public void writetofile(string msg3, string path33)//从第0位置开始更改,用于变量更改
{
FileStream fs3 = File.Create(path33);
byte[] contents3 = Encoding.Default.GetBytes(msg3);
fs3.Write(contents3, 0, contents3.Length);
fs3.Close();
}
// public void stringtofile(string msg4,string )//更改位置可变,用于对日记内容记录
// {
//}
public void choice(string c)
{
string str2 = c.Substring(0, 4); // 第一个是索引位置,第二个是索引长度
if (str2 == "talk")
{
Console.WriteLine("OK,how are you today");
for (i = 0; i <= 1000; i++)
{
tmp1 = Console.ReadLine();
if (tmp1.Substring(0, 1) == "!")
{
break;
}
else
{
liaotian_words(tmp1);
}
}
}
else//写日记
if(str2 == "plan")
{
Console.WriteLine("write or watch");
string str3 = Console.ReadLine();
string str4 = str3.Substring(0, 5);
if ( "write" == str4)
{
writediary(DateTime.Now.ToString("dddd,dd MMMM,yyyy"), path08);
for (i = 0; i <= 1000; i++)
{
tmp = Console.ReadLine();
if (tmp.Substring(0, 1) == "!")
{
break;
}
else
{
writediary(tmp, path08);
writediary(tmp, path09);
}
}
}
else
{
Console.WriteLine ( File.ReadAllText(path08, Encoding.Default));
}
}
else
{
Console.WriteLine("亲爱的天天!和我分享你今天的生活吧!嘻嘻嘻!");
writediary(DateTime.Now.ToLocalTime().ToString(), path06);
writediary((File.ReadAllText(path05, Encoding.Default)), path06);
for (i = 0; i <= 1000; i++)
{
tmp = Console.ReadLine();
if (tmp.Substring(0, 1) == "!")
{
break;
}
else
{
writediary(tmp, path06);
}
}
}
}
public void writediary(string st3,string st4)//这里面的东西!!!在下一行输入!!!找了一个小时才在网上找到
{
FileStream fs4 = new FileStream(st4, FileMode.Append);
StreamWriter sw = new StreamWriter(fs4);
sw.WriteLine(st3);
sw.Close();
fs4.Close();
}
public void plandui1()
{
string shuru0 = File.ReadAllText(path09, Encoding.Default);
string[] strs2 = shuru0.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string item in strs2)
{
Console.WriteLine("做完下面这件事了么?");
Console.WriteLine(item);
if(Console.ReadLine() == "yes" )
{
string[] lines = System.IO.File.ReadAllLines(path09);//用于删除某一行
//转换
List<string> list = new List<string>();
list.AddRange(lines);
list.RemoveAt(0);
lines = list.ToArray();
//保存
System.IO.File.WriteAllLines(path09, lines);
}
else
{
Console.WriteLine("赶紧去做吧!");
}
}
}
public void liaotian_words(string shuru1)
{
int liaoi;
liaoi = 0;
writediary("gf", path07);
writediary(shuru1, path07);
shuru1 = shuru1.ToLower();//s.ToLower():返回值为字符串的小写
string[] strs = shuru1.Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
//循环打印字符串数组strs中的内容
foreach (string item in strs)
{
liaoi = liaoi + 1;
}
if(liaoi == 3)
{
liaoi = 0;
foreach (string item in strs)
{
liaoi = liaoi + 1;
if(liaoi == 1)
{
guanlian_n(item);
}
}
}
else
{
if(liaoi == 1)
{
}
else
{
if(liaoi == 4)
{
}
}
}
}
public void liaotian_adj(string shuru2)
{
}
public void liaotian_v(string shuru3)
{
}
public void liaotian_n(string shuru4)
{
}
public int guanlian_n(string shuru5)
{
string[] strs1 = shuru5.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
int i0 = 0;
foreach (string item in strs1)
{
i0 = i0 + 1;
// if(strs1 == shuru5)
// {
;
// return 0;
// }
//else
// {
// return 1;
// }
}
return 2;
}
public void guanlian_v(string shuru6)
{
}
public void guanlian_adj(string shuru7)
{
}
}
}
学习文件操作的代码。。。
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
namespace 文件创建复制移动删除操作
{
class Program
{
static void ceshi()
{
//设置控制台标题和指明文件路径
Console.Title = "文件创建、复制、移动、删除操作";
string path1 = "1.txt";
string path2 = "2.txt";
string path3 = "d:\\3.txt";
string path4 = @"c:\4.txt";
//要写入的文本字符串
string msg = "文件基本操作示例";
msg += Environment.NewLine + " A.创建文件;B.复制文件;C.移动文件;D:删除文件;";//Environment.NewLine属性用于获取为当前环境定义的换行字符串
//在path1指定的路径中创建或覆盖文件,并将msg字符串写入文件中。
FileStream fs = File.Create(path1);//创建文件流,在path1指定的路径中创建或覆盖文件
byte[] contents = Encoding.Default.GetBytes(msg);//将msg字符串的字符编码变成当前ANSI代码页的编码并保存到byte数组中
fs.Write(contents, 0, contents.Length);//从contents中读取字符串,从0位置开始将字符串长度大小的字符串写入到文本中。
fs.Close();//关闭文件流
//打印写入paht1路径下文件内容
string str = File.ReadAllText(path1, Encoding.Default);//读取path1指定的文件中的所有文本内容保存到字符串str中中。
Console.WriteLine("1.txt已经被成功创建!\n\n");
Console.WriteLine("\t创建1.txt并写入文件,其内容如下:\n\n");
Console.WriteLine(" " + str);
Console.ReadKey();
//将path1路径下文件复制到path2指定的路径
if (File.Exists(path2))//如果path2下有同名文件就将其删除
File.Delete(path2);
File.Copy(path1, path2);//将path1路径下的文件复制到文件path2路径下的文件
Console.WriteLine("\n1.txt被成功复制出一个2.txt");
Console.ReadKey();
//将path1路径下文件移动到path3指定的路径
File.Move(path1, path3);
Console.WriteLine("\n1.txt文件被成功移到D盘根目录并重命名为3.txt");
Console.ReadKey();
//将path2路径下文件移动到path4指定的路径
File.Move(path2, path4);
Console.WriteLine("\n2.txt一个文件被成功移动到C盘并更名为4.txt");
Console.ReadKey();
//删除三个文件
File.Delete(path2);
File.Delete(path3);
File.Delete(path4);
Console.WriteLine("\n三个文件被成功删除。");
Console.ReadKey();
}
}
}