Visual C#程序设计技能总结

来源:互联网 发布:win2012更改网络类型 编辑:程序博客网 时间:2024/05/22 15:50

 Visual C#程序设计技艺总结
2008-十-04
  获取资料的版本信息:
FileVersionInfo myFileVersionInfo一 = FileVersionInfo.GetVersionInfo("D://TEST.DLL");
textBox一.Text="版本号: " + myFileVersionInfo一.FileVersion;

  更动资料属性,剔除只读资料:

  下例欲将E:/test.txt资料拷贝至D:/tmp/test.txt,但D:/tmp/test.txt已经存在。

//File.Copy(sourceFile,destinationFile,true); 用以拷贝资料
//应destinationFile已经存在时,没法将资料file一拷贝到目标资料,
//因而先剔除destination资料,File.Delete()步骤不能剔除只读资料,
//因而,如其资料属性为只读(Attributes属性中会包孕有"ReadOnly"),
//先把资料属性重置为Normal,其后再剔除:
string file一="E://test.txt";
string destinationFile="d://tmp//test.txt";
if(File.Exists(destinationFile))
{
  FileInfo fi=new FileInfo(destinationFile);
  if(fi.Attributes.ToString().IndexOf("ReadOnly")!=-一)
  fi.Attributes=FileAttributes.Normal;
  File.Delete(destinationFile);
}
File.Copy(file一,destinationFile,true);

  C#中字符串的格式化及转换成数值的步骤

  字符串转换成数目字,例如"1234"转换成数目字1234:

string str="1234";
int i=Convert.ToInt32(str);

  格式化字符串,向长度小于30的字符串末后增添特定字符,补足n个字符,运用String种的PadRight(int,char)步骤:

String str="1234";
str=str.PadRight(30,' ') //向长度小于30的字符串终了平添空格,补足30个字符

  按行读写资料

  判断资料是不是存在:File.Exists(string filePath)

  判断索引是不是存在irectory.Exists("D://LastestVersion")

  按行读取资料:

int fileCount=零;
// Open the file just specified such that no one else can use it.
StreamReader sr = new StreamReader(textBox一.Text.Trim());
while(sr.Peek() > -一)//StreamReader.Peek()回来下一个可用字符,但不运用它
{
  listBox一.Items.Add(sr.ReadLine());
  fileCount++;
}
sr.Close();

  按行写下资料:

StreamWriter sw = new StreamWriter("D://result.txt");
for(int i=零;i<十;i++)
{
  sw.WriteLine("这是第"+i.ToString()+"行数据");
}

  资料索引对话框的运用

  资料对话框即过滤条件的应用:

string resultFile="";
OpenFileDialog openFileDialog一 = new OpenFileDialog();
openFileDialog一.InitialDirectory = "D://Patch" ;
openFileDialog一.Filter = "All files (*.*)|*.*|txt files (*.txt)|*.txt" ;
openFileDialog一.FilterIndex = 二 ;
openFileDialog一.RestoreDirectory = true ;
if(openFileDialog一.ShowDialog() == DialogResult.OK)
resultFile=openFileDialog一.FileName;

  索引对话框的施用:

string resultFolder="";
FolderBrowserDialog openFolderDialog一=new FolderBrowserDialog();
openFolderDialog一.RootFolder=Environment.SpecialFolder.MyComputer;
if(openFolderDialog一.ShowDialog()==DialogResult.OK)
resultFolder=openFolderDialog一.SelectedPath;
本文来源:
我的异常网
Java Exception
Dotnet Exception
Oracle Exception

原创粉丝点击