FileInfo操作文件属性
FileInfo fileInfo = new FileInfo('helloworld.txt');
fileInfo Attributes = FileAttributes.Hidden;//Attributes属性,后面设置的能够覆盖掉原来的;
删除文件
抽象类
abstract class interface IMyInterface//声明
{
protected abstract void Text();
}
class TestInterface : IMyInterface{}//只能同时继承一个类
接口和抽象方法的区别:
C#文件操作
C#预处理指令
Path类:对路径进行操作
读取内容
创建文件
FilezStream fileStream = new FileStream
(
'helloword.txt',FileMode.
Append打开现有文件,并将光标定位在最后面,可以往文件里面追加一些内容,如果这个文件不存在就创建一个新的文件/
Create穿件一个文件,如果这个文件已经有的话就删掉旧的文件然后穿件一个新的文件/
CrealeNew创建一个新的文件,如果这个文件存在就抛出一个异常/
Open打开一个文件,如果这个文件不存在就报错/
OpenOrCreate打开或者创建,若不存在就创建一个,若存在就打开当前文件/
Truncate打开一个文件,并删除文件里的内容,如果没有文件就会抛出一个异常
)
写入字符串
FilezStream fileStream = new FileStream('helloword.txt',FileMode.OpenOrCreate,FileAccess.Read/ReadWrite/Write);//FileAccess.Read/ReadWrite/Write)访问权限,读,读写,写
//写
string content = 'hello word!!!'; fileStream.Write(ENcoding,UTF8.GetBytes(content),0,ENcoding,UTF8.GetBytes(content).Length);//ENcoding将字节转为字符串,GetBytes方法:将一个字符串转为一个字节数组
fileStream.Close();//关闭
//读
byte[]content = new byte[fileStream.Length];
fileStream.Read(content,0,content.Length);
Console.WriteLine('读取成功!' + Encoding.UTF8.GetString(content));
自定义异常
class UserNameOrPasswordErrorException : Exception
{
public UserNameOrPasswordErrorException(string message) : base('用户名或者密码错误!')
{
}
}
class Program
{
static void Login(string name,string password)
{
if(name == '123'&&password == '123')
}
else{
throw new UserNameOrPasswordErrorException();
}
static void Main(string[] args)
{
string name,password;
Console.WriteLine('请输入用户名');
name = Console.ReadLine();
Console.WriteLine('请输入密码');
name = Console.ReadLine();
try
{
Login(name,password;)
}
catch(UserNameOrPasswordErrorException)
{
Console.WriteLine(c.Message);
//给用户一个提示
}
catch(Exception e)
{
}
finally//不管用户输入对错与否都会跳到这里
{
Console.WriteLine('操作结束!');
}
}
}
任何类型的异常都可以通过Exception捕捉。
@#error#warning
#error '这里有一个错误'
using System;
#define DEBUG//版本1
#define RELESSE//版本2
#if DEBUG &&RELEASE
#error 'DEBUG和RELEASE不能同时定义'
#endif
#warning '这是一个警告!'
#region 属性//对一串代码进行标记,没有特殊含义
public string name;
public int age;
#endregion//对一串代码进行标记,没有特殊含义
多个版本,可以通过预处理的指令,控制指定的代码
#define TestDef
#undef TestDef//取消预定义
using System;
namespace XXX
{
class Program
{
static void Main(string[] args)
{
#if(TestDef)
Console.WriteLine('TestDef is defined');
#else
Console.WriteLine('TestDef is not defined')
#endif
}
}
}
Fil文件的复制和移动等
写入内容
string[] str = new string[] {'helloworld1','helloworld2','helloworld3'}
File.WriteAllLines('helloworld.txt',str);
和抽象类有相似之处,都是只定义一个规则,本身不处理任何事情,在接口中可以定义,属性,方法,事件
interface IMyInterface2
{
void Text2();
}
interface IMyInterface//声明
{
void Text();
}
class TestInterface : IMyInterface,IMyInterface2 //实现了接口中的方法,继承多个接口
{
public void Test()
{
Console.WriteLine('Text Interface Method');
}
public void Test2()
{
Console.WriteLine('Text Interface Method2');
}
}
class Program
{
static void Main(string[] args)
{
TextOmterface textOmterface = new TestInterface();
textInterface.Text();
textInterface.Text2();
Console.WriteLine();
}
}
抽象类是一个类,一个类只能有一个父亲,不能同事继承多个类,接口可以在一个类实现多个接口用以实现多重的继承。
异常处理
try
{
Console.WriteLIne(a[10]);
}
catch(错误提示)
{
Console.WriteLine('提示文字(自编辑)');
}
try
{
Console.WriteLine(a/b);//想要检测错误的代码
}
catch(Exception e)//Exception是所有异常错误的基类
{
Console.WriteLine('数据输入错误,请重新输入');
}
在编译之前就能进行一些处理
#define TestDef
using System;
namespace XXX
{
class Program
{
static void Main(string[] args)
{
#if(TestDef)
Console.WriteLine('TestDef is defined');
#else
Console.WriteLine('TestDef is not defined')
#endif
}
}
}
文章为作者独立观点,不代表股票交易接口观点