读写txt文件
using System.IO;
private static string ReadFile()
{
string var="";
using (FileStream fs = new FileStream("d://wei.Txt", FileMode.Open, FileAccess.Read))
{
StreamReader sr = new StreamReader(fs, Encoding.Default);
var = var + sr.ReadToEnd();
sr.Close();
}
return var;
}
//写文件
private void WriteFile(string ph, string varchar)
{
using (FileStream fs = new FileStream(ph, FileMode.Create, FileAccess.Write))
{
StreamWriter sr = new StreamWriter(fs);
StringBuilder sb = new StringBuilder();
//for (int i = 0; i < 10000; i++)
// sb.Append(i.ToString("0000") + ",");
sb.Append(varchar);
sr.Write(sb.ToString());
sr.Close();
}
}
打开一个进程
using System.Diagnostics;
private void button4_Click(object sender, EventArgs e)
{
Process myprocess = new Process();
myprocess.StartInfo.FileName = "D://wei.txt";
myprocess.Start();
}
操作一个服务
using System.ServiceProcess;
private void Form1_Load(object sender, EventArgs e)
{
ServiceController sc = new ServiceController("MSSQLSERVER", "MYWINXP-SP2");
sc.Stop();
}
查文件基本用法
using system.IO;
private void button8_Click(object sender, EventArgs e)
{
this.comboBox1.Items.Clear();
this.comboBox1.Items.AddRange(Directory.GetLogicalDrives());
this.comboBox1.Text = comboBox1.Items[0].ToString();
}
private void button9_Click(object sender, EventArgs e)
{
this.comboBox2.Items.Clear();
System.IO.DirectoryInfo dt = new DirectoryInfo(comboBox1.Text);
this.comboBox2.Items.AddRange(dt.GetFiles());
this.comboBox2.Text = comboBox2.Items[0].ToString();
}
为删除和拷贝文件或文件夹写的两个方法
/// 删除文件夹DeleteFile(@"D:/netdemo")
/// <summary>
/// 删除文件夹
/// </summary>
/// <param name="ph">要删的文件或文件夹路径</param>
/// <returns>无返回</returns>
private void DeleteFile(string ph)
{//是否为文件
if (File.Exists(ph))
{
try
{//删除文件
File.Delete(ph);
}
catch//权限不允许删除
{//修改为正常文件
File.SetAttributes(ph, FileAttributes.Normal);
File.Delete(ph);
}
}
else if (Directory.Exists(ph))//是否为文件夹
{
DirectoryInfo dtif = new DirectoryInfo(ph);//资源文件夹
foreach (FileSystemInfo fi in dtif.GetFileSystemInfos())//检索所有子项
DeleteFile(fi.FullName);//回掉方法
try
{
Directory.Delete(ph, true);//删除文件夹
}
catch//权限不允许删除
{
dtif.Attributes = FileAttributes.Normal;//修改为正常文件夹
Directory.Delete(ph, true);//删除文件夹
}
}
}
/// 将一文件或文件夹拷贝至另一文件或文件夹
/// <summary>
/// 将一文件或文件夹拷贝至另一文件夹
/// </summary>
/// <param name="phfo">文件或文件夹资源路径</param>
/// <param name="phto">拷贝到的文件夹路径</param>
/// <param name="mode">是否为复盖当级文件夹true为复盖</param>
/// <returns>无返回</returns>
private void CpFile(string phfo, string phto, bool mode)
{
//是否为文件
if (File.Exists(phfo))
{//资源文件
FileInfo fif = new FileInfo(phfo);
try
{//复制到目的地
fif.CopyTo(phto, true);
}
catch//目标文件权限不允许改写
{//调用自定义方法删除文件并复制到目的地
DeleteFile(phto);
fif.CopyTo(phto);
}
}
if (mode && Directory.Exists(phfo))//是否为文件夹
{
DirectoryInfo dtif = new DirectoryInfo(phfo);//资源文件夹
DirectoryInfo diry = new DirectoryInfo(phto);//目地文件夹
if (!diry.Exists)//目的是否存在
diry.Create();//创建文件夹
foreach (FileSystemInfo fi in dtif.GetFiles())//检索所有子项
CpFile(fi.FullName, phto + "//" + fi.Name, true);//回调方法
foreach (FileSystemInfo fi in dtif.GetDirectories())
CpFile(fi.FullName, phto);
diry.Attributes = dtif.Attributes;//为目地文件赋上原资源文件权限
}
else if (Directory.Exists(phfo))//是否为文件夹
{
DirectoryInfo dtif = new DirectoryInfo(phfo);//资源文件夹
DirectoryInfo diry = new DirectoryInfo(phto);//目地文件夹
if (diry.Exists)//目的是否存在
DeleteFile(diry.FullName);//自定义方法删除文件夹
diry.Create();//创建文件夹
foreach (FileSystemInfo fi in dtif.GetFileSystemInfos())//检索所有子项
CpFile(fi.FullName, phto + "//" + fi.Name, false);//回调方法
diry.Attributes = dtif.Attributes;//为目地文件赋上原资源文件权限
}
}
/// 将一文件或文件夹拷贝至另一文件夹内CpFile( @"D:/netdemo",@"C:/www");
/// <summary>
/// 将一文件或文件夹拷贝至另一文件夹内
/// </summary>
/// <param name="phfo">文件或文件夹资源路径</param>
/// <param name="phto">拷贝到的文件夹路径</param>
/// <returns>无返回</returns>
private void CpFile(string phfo, string phto)
{//是否为文件
if (File.Exists(phfo))
{//资源文件
FileInfo fif = new FileInfo(phfo);
try
{//复制到目的地
fif.CopyTo(phto + "//" + fif.Name, true);
}
catch//目标文件权限不允许改写
{//调用自定义方法删除文件并复制到目的地
DeleteFile(phto + "//" + fif.Name);
fif.CopyTo(phto + "//" + fif.Name);
}
}
else if (Directory.Exists(phfo))//是否为文件夹
{
DirectoryInfo dtif = new DirectoryInfo(phfo);//资源文件夹
DirectoryInfo diry = new DirectoryInfo(phto + "//" + dtif.Name);//目地文件夹
if (diry.Exists)//目的是否存在
DeleteFile(diry.FullName);//自定义方法删除文件夹
diry.Create();//创建文件夹
foreach (FileSystemInfo di in dtif.GetFileSystemInfos())//检索所有子项
CpFile(di.FullName, phto + "//" + dtif.Name);//回调方法
diry.Attributes = dtif.Attributes;//为目地文件赋上原资源文件权限
}
}
/// 查找一个文件下的所有文件并排列,和删出过期文件
/// <summary>
/// 查找一个文件下的所有文件并排列,和删出过期文件
/// </summary>
/// <param name="ph">要整理的文件夹</param>
/// <param name="le">有效文件个数</param>
/// <returns>返回有效文挡</returns>
private string[] sdeph(string ph, int le)
{
DirectoryInfo[] alldir = new DirectoryInfo(ph).GetDirectories();
if (le > alldir.Length)
le = alldir.Length;
string[] sestr = new string[alldir.Length];
for (int ii = 0; ii < alldir.Length; ii++)
{
sestr[ii] = alldir[ii].Name;
}
string[] restr = new string[le];
string var;
for (int j = 0; j < sestr.Length-1; j++)
for (int i = j+1; i < sestr.Length; i++)
{
if (DateTime.Parse(sestr[j]) < DateTime.Parse(sestr[i]))
{
var = sestr[i];
sestr[i] = sestr[j];
sestr[j] = var;
}
else
{
var = sestr[j];
}
}
for (int i = 0; i < sestr.Length; i++)
{
if (i < le)
restr[i] = sestr[i];
else
DeleteFile(textBox1.Text + "//" + sestr[i]);
}
return restr;
}
dataset拿excel
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source = d://wei.xls;Extended Properties=Excel 8.0";
string strCom = " SELECT * FROM [sheet1$] ";
DataSet ds = new DataSet();
OleDbDataAdapter myConn = new OleDbDataAdapter(strCom, new OleDbConnection(strCon));
myConn.Fill(ds);
线程
///让另一线程做此事
/// <summary>
/// 让另一线程做此事
/// </summary>
public void NewThread()
{
Thread t = new Thread(delegate()
{
System.Threading.Thread.Sleep(5000);//阻塞当前线程5秒
MessageBox.Show("OK!");
});
t.IsBackground = true;//设为后台进程
t.Start();//起动进程
}
利用sqldmo做sql备份还原的两个方法
--之前需导入com组件Microsoft sqldmo object library
public static void DbBackup()
{
SQLDMO.Backup oBackup = new SQLDMO.BackupClass();
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
try
{
oSQLServer.LoginSecure = false;
oSQLServer.Connect(".", "sa", "");
oBackup.Action = SQLDMO.SQLDMO_BACKUP_TYPE.SQLDMOBackup_Database;
oBackup.Database = "dataqq";
oBackup.Files = @"d:/dataqq.bak";
oBackup.BackupSetName = "dataqq";
oBackup.BackupSetDescription = "数据库备份";
oBackup.Initialize = true;
oBackup.SQLBackup(oSQLServer);
}
catch
{
throw;
}
finally
{
oSQLServer.DisConnect();
}
}
--调用之前需保证没有该数据库的其它连接
public static void DbRestore()
{
SQLDMO.Restore oRestore = new SQLDMO.RestoreClass();
SQLDMO.SQLServer oSQLServer = new SQLDMO.SQLServerClass();
try
{
oSQLServer.LoginSecure = false;
oSQLServer.Connect(".", "sa", "");
oRestore.Action = SQLDMO.SQLDMO_RESTORE_TYPE.SQLDMORestore_Database;
oRestore.Database = "dataqq";
oRestore.Files = @"D:/dataqq.bak";
oRestore.ReplaceDatabase = true;
oRestore.SQLRestore(oSQLServer);
}
catch
{
throw;
}
finally
{
oSQLServer.DisConnect();
}
}