下面是一段ftp下载的代码,下载完后的文件依然被进程占用,我想移动这个文件,一直提示,这个文件被占用!
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;namespace Update
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static string FileName = System.Windows.Forms.Application.StartupPath + "\\System\\Sys.ini";
public static string downpath = System.Windows.Forms.Application.StartupPath + "\\temp";
[System.Runtime.InteropServices.DllImport("kernel32")]
public static extern long WritePrivateProfileString(string section, string key,
string val, string filePath); /*参数说明:section:INI文件中的段落;key:INI文件中的关键字;val:INI文件中关键字的数值;
filePath:INI文件的完整的路径和名称。*/ [System.Runtime.InteropServices.DllImport("kernel32")]
public static extern int GetPrivateProfileString(string section, string key, string def,
StringBuilder retVal, int size, string filePath); /*参数说明:section:INI文件中的段落名称;key:INI文件中的关键字;def:无法读取时候时候的缺省
数值;retVal:读取数值;size:数值的大小;filePath:INI文件的完整路径和名称。*/
int UDTimes = 0;
//
private void button1_Click(object sender, EventArgs e)
{
if (label2.Text == label4.Text)
{
MessageBox.Show("已经是最新版本!","提示",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
}
else
{
progressBar1.Visible = true;
Down();
StringBuilder temp2 = new StringBuilder(255);
GetPrivateProfileString("version", "NewVs", "服务器地址读取错误。", temp2, 255, FileName);
string NewVs = temp2.ToString(); WritePrivateProfileString("version", "Vs", NewVs, FileName);
WritePrivateProfileString("version", "Flag", "1", FileName);
if (MessageBox.Show("升级已完成,重新进入订单系统?","提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk)
== DialogResult.OK)
{
CopyFile(downpath , System.Windows.Forms.Application.StartupPath );
}
}
} private void Form1_Load(object sender, EventArgs e)
{
// timer1.Enabled = true;
StringBuilder temp2 = new StringBuilder(255); GetPrivateProfileString("version", "Vs", "服务器地址读取错误。", temp2, 255, FileName);
string ver = temp2.ToString();
GetPrivateProfileString("version", "NewVs", "服务器地址读取错误。", temp2, 255, FileName);
string NewVs = temp2.ToString();
GetPrivateProfileString("version", "Flag", "服务器地址读取错误。", temp2, 255, FileName);
string Flag = temp2.ToString(); this.label2.Text = ver;
this.label4.Text = NewVs;
if (label2.Text == label4.Text)
{
timer1.Enabled = false;
}
else
{
timer1.Enabled = true;
}
} private void CopyFile(string StartPath, string EndPath)
{
System.IO.File.Delete(EndPath); FileStream fs = new FileStream(StartPath, FileMode.Open);
//定义内存缓冲区
byte[] buffer = new byte[(int)fs.Length];
//将文件读入到内存缓冲区中
fs.Read(buffer, 0, buffer.Length);
fs.Close(); //定义一个文件流,用于将缓冲区中的文件流写入文件
FileStream fs1 = new FileStream(EndPath, FileMode.Open);
//写入文件
fs1.Write(buffer, 0, buffer.Length);
fs1.Close();
} private void timer1_Tick(object sender, EventArgs e)
{
StringBuilder temp2 = new StringBuilder(255); GetPrivateProfileString("version", "Vs", "服务器地址读取错误。", temp2, 255, FileName);
string ver = temp2.ToString();
GetPrivateProfileString("version", "NewVs", "服务器地址读取错误。", temp2, 255, FileName);
string NewVs = temp2.ToString();
GetPrivateProfileString("version", "Flag", "服务器地址读取错误。", temp2, 255, FileName);
string Flag = temp2.ToString(); label2.Text = ver.ToString();
label4.Text = NewVs.ToString();
UDTimes = UDTimes + 1; if (label2.Text == label4.Text)
{
timer1.Enabled = false;
}
else
{
progressBar1.Visible = true; Down();
StringBuilder temp = new StringBuilder(255);
GetPrivateProfileString("version", "NewVs", "服务器地址读取错误。", temp2, 255, FileName);
string NewVs1 = temp2.ToString(); WritePrivateProfileString("version", "Vs", NewVs1, FileName);
WritePrivateProfileString("version", "Flag", "1", FileName); }
}
//down方法:
private void Down()
{
FtpDown ftp = new FtpDown();
string last = ""; string[] name = new string[] { }; name = ftp.GetFileList(); for (int i = 0; i < name.Length; i++)
{
if (name[i].ToString().StartsWith("Client_CT"))
{
string FileName1 = name[i].ToString(); if (System.IO.File.Exists(downpath + "\\" + name[i].ToString()))
{
System.IO.File.Delete(downpath + "\\" + name[i].ToString());
}
ftp.Download(downpath, FileName1,progressBar1, out last); }
}
ftp.CloseFtp();
//ftp.Dispose(); //ftp.Close();
Thread.Sleep(1000);
} private void timer2_Tick(object sender, EventArgs e)
{
//if (System.IO.File.Exists(downpath + "\\Client_CT.exe"))
//{ // timer2.Enabled = false;
// if (MessageBox.Show("升级已完成,重新进入订单系统?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk)
// == DialogResult.OK)
// {
// CopyFile(downpath + "\\Client_CT", System.Windows.Forms.Application.StartupPath + "\\Client_CT");
// }
// progressBar1.Visible = false;
//}
} }
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.IO;namespace Update
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
public static string FileName = System.Windows.Forms.Application.StartupPath + "\\System\\Sys.ini";
public static string downpath = System.Windows.Forms.Application.StartupPath + "\\temp";
[System.Runtime.InteropServices.DllImport("kernel32")]
public static extern long WritePrivateProfileString(string section, string key,
string val, string filePath); /*参数说明:section:INI文件中的段落;key:INI文件中的关键字;val:INI文件中关键字的数值;
filePath:INI文件的完整的路径和名称。*/ [System.Runtime.InteropServices.DllImport("kernel32")]
public static extern int GetPrivateProfileString(string section, string key, string def,
StringBuilder retVal, int size, string filePath); /*参数说明:section:INI文件中的段落名称;key:INI文件中的关键字;def:无法读取时候时候的缺省
数值;retVal:读取数值;size:数值的大小;filePath:INI文件的完整路径和名称。*/
int UDTimes = 0;
//
private void button1_Click(object sender, EventArgs e)
{
if (label2.Text == label4.Text)
{
MessageBox.Show("已经是最新版本!","提示",MessageBoxButtons.OK,MessageBoxIcon.Asterisk);
}
else
{
progressBar1.Visible = true;
Down();
StringBuilder temp2 = new StringBuilder(255);
GetPrivateProfileString("version", "NewVs", "服务器地址读取错误。", temp2, 255, FileName);
string NewVs = temp2.ToString(); WritePrivateProfileString("version", "Vs", NewVs, FileName);
WritePrivateProfileString("version", "Flag", "1", FileName);
if (MessageBox.Show("升级已完成,重新进入订单系统?","提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk)
== DialogResult.OK)
{
CopyFile(downpath , System.Windows.Forms.Application.StartupPath );
}
}
} private void Form1_Load(object sender, EventArgs e)
{
// timer1.Enabled = true;
StringBuilder temp2 = new StringBuilder(255); GetPrivateProfileString("version", "Vs", "服务器地址读取错误。", temp2, 255, FileName);
string ver = temp2.ToString();
GetPrivateProfileString("version", "NewVs", "服务器地址读取错误。", temp2, 255, FileName);
string NewVs = temp2.ToString();
GetPrivateProfileString("version", "Flag", "服务器地址读取错误。", temp2, 255, FileName);
string Flag = temp2.ToString(); this.label2.Text = ver;
this.label4.Text = NewVs;
if (label2.Text == label4.Text)
{
timer1.Enabled = false;
}
else
{
timer1.Enabled = true;
}
} private void CopyFile(string StartPath, string EndPath)
{
System.IO.File.Delete(EndPath); FileStream fs = new FileStream(StartPath, FileMode.Open);
//定义内存缓冲区
byte[] buffer = new byte[(int)fs.Length];
//将文件读入到内存缓冲区中
fs.Read(buffer, 0, buffer.Length);
fs.Close(); //定义一个文件流,用于将缓冲区中的文件流写入文件
FileStream fs1 = new FileStream(EndPath, FileMode.Open);
//写入文件
fs1.Write(buffer, 0, buffer.Length);
fs1.Close();
} private void timer1_Tick(object sender, EventArgs e)
{
StringBuilder temp2 = new StringBuilder(255); GetPrivateProfileString("version", "Vs", "服务器地址读取错误。", temp2, 255, FileName);
string ver = temp2.ToString();
GetPrivateProfileString("version", "NewVs", "服务器地址读取错误。", temp2, 255, FileName);
string NewVs = temp2.ToString();
GetPrivateProfileString("version", "Flag", "服务器地址读取错误。", temp2, 255, FileName);
string Flag = temp2.ToString(); label2.Text = ver.ToString();
label4.Text = NewVs.ToString();
UDTimes = UDTimes + 1; if (label2.Text == label4.Text)
{
timer1.Enabled = false;
}
else
{
progressBar1.Visible = true; Down();
StringBuilder temp = new StringBuilder(255);
GetPrivateProfileString("version", "NewVs", "服务器地址读取错误。", temp2, 255, FileName);
string NewVs1 = temp2.ToString(); WritePrivateProfileString("version", "Vs", NewVs1, FileName);
WritePrivateProfileString("version", "Flag", "1", FileName); }
}
//down方法:
private void Down()
{
FtpDown ftp = new FtpDown();
string last = ""; string[] name = new string[] { }; name = ftp.GetFileList(); for (int i = 0; i < name.Length; i++)
{
if (name[i].ToString().StartsWith("Client_CT"))
{
string FileName1 = name[i].ToString(); if (System.IO.File.Exists(downpath + "\\" + name[i].ToString()))
{
System.IO.File.Delete(downpath + "\\" + name[i].ToString());
}
ftp.Download(downpath, FileName1,progressBar1, out last); }
}
ftp.CloseFtp();
//ftp.Dispose(); //ftp.Close();
Thread.Sleep(1000);
} private void timer2_Tick(object sender, EventArgs e)
{
//if (System.IO.File.Exists(downpath + "\\Client_CT.exe"))
//{ // timer2.Enabled = false;
// if (MessageBox.Show("升级已完成,重新进入订单系统?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Asterisk)
// == DialogResult.OK)
// {
// CopyFile(downpath + "\\Client_CT", System.Windows.Forms.Application.StartupPath + "\\Client_CT");
// }
// progressBar1.Visible = false;
//}
} }
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货