VisualC#程序设计技巧小结
获取文件的版本信息:
file version info my file version info 1 = file version info。GetVersionInfo("D:\TEST。DLL”);
textBox1。Text= "版本号:"+myfile version info 1 . file version;
更改文件属性并删除只读文件:
以下示例希望将E:\test.txt文件复制到D:\tmp\test.txt,但D:\tmp\test.txt已经存在。
//文件。Copy(sourceFile,destinationFile,true);用于复制文件
//当目的文件已经存在时,文件file1无法复制到目的文件,
//所以要先删除目的文件,文件。Delete()方法无法删除只读文件。
//因此,如果文件属性是只读的(“ReadOnly”会包含在Attributes属性中),
/首先将文件属性重置为正常,然后删除:
string file 1 = " e:\ \ test . txt ";
string destination file = " d:\ \ tmp \ \ test . txt ";
if(文件。exists(destination file))
{
FileInfo fi = new FileInfo(destination file);
if(fi。Attributes.ToString()。IndexOf("ReadOnly ")!=-1)
fi。属性=文件属性。正常;
文件。delete(destination file);
}
文件。Copy(file1,destinationFile,true);
C语言中字符串的格式及其转换成数值的方法
将字符串转换为数字,例如将“1234”转换为数字1234:
string str = " 1234
int i=Convert。toint 32(str);
格式化一个字符串,在长度小于30的字符串末尾添加一个特定字符,组成n个字符。使用String类的PadRight(int,char)方法:
String str = " 1234
str = str.padright (30,' ')//在长度小于30的字符串末尾加上空,补足30个字符。
逐行读写文件。
确定文件是否存在。存在(字符串文件路径)
确定目录是否存在:directory . exists(" d:\ \ lastestversion ")
逐行读取文件:
int file count = 0;
//打开刚刚指定的文件,这样其他人就不能使用它。
StreamReader Sr = new StreamReader(textbox 1。text . Trim());
while(Sr . peek()>-1)//streamreader . peek()返回下一个可用字符,但不使用它
{
listbox 1 . items . add(Sr . readline());
filecount++;
}
Sr . Close();
逐行写入文件:
StreamWriter sw = new StreamWriter("D:\\result.txt");
for(int i=0;i
0条评论