没时间整理:
namespace ClickOnce
{
public partial class Form1 : Form
{
private string localVersion;
private ApplicationDeployment deploy;
private Dictionary<string, string> dllMapping = new Dictionary<string, string>();
public Form1()
{
InitializeComponent();
dllMapping.Add("OnDemandAssembly", "DemandAssembly01");
AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);
deploy = ApplicationDeployment.CurrentDeployment;
deploy.UpdateProgressChanged += new DeploymentProgressChangedEventHandler(deploy_UpdateProgressChanged);
}
Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
// 可以加上你的权限管理
Assembly needAssembly = null;
if (ApplicationDeployment.IsNetworkDeployed)
{
string[] nameParts = args.Name.Split(',');
string dllName = nameParts[0];
MessageBox.Show("AsseblyResolve Begin");
MessageBox.Show(nameParts[0]);
string dowloadGroupName = dllMapping[dllName];
try
{
deploy.DownloadFileGroup(dowloadGroupName);
}
catch (DeploymentException de)
{
MessageBox.Show("Error Download file {0}",dllName);
}
//下载后加重新LoadAssembly
needAssembly = Assembly.LoadFile(Application.StartupPath + @"/" + dllName + ".dll");
}
return (needAssembly);
}
void deploy_UpdateProgressChanged(object sender, DeploymentProgressChangedEventArgs e)
{
//this.progressBar1.Value =
this.Text = String.Format("Downloading file group {0}; {1:D}K of {2:D}K completed.", e.Group, e.BytesCompleted / 1024, e.BytesTotal / 1024);
}
private void Form1_Load(object sender, EventArgs e)
{
LocalAppInfo();
this.textBox1.Text = Application.StartupPath;
DeployServerInfo();
if (deploy.IsFirstRun)
{
MessageBox.Show("Thanks for choose kw");
}
}
private void LocalAppInfo()
{
this.localVersion = Assembly.GetExecutingAssembly().GetName().Name;
this.localVersion += Assembly.GetExecutingAssembly().GetName().Version.ToString();
}
private void DeployServerInfo()
{
this.label3.Text += deploy.DataDirectory;
}
private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show("开始更新检测");
this.lbServerVersion.Text = deploy.CurrentVersion.ToString();
UpdateCheckInfo checkInfo = deploy.CheckForDetailedUpdate();
if (checkInfo.UpdateAvailable)
{
deploy.Update();
MessageBox.Show("Update Ok");
Application.Restart();
}
//if (deploy.CheckForUpdate())
//{
// deploy.UpdateAsync();
//}
//else
//{
// this.Text = "the version that running is the newest verions";
//}
}
private void button2_Click(object sender, EventArgs e)
{
DynamicClass dc = new DynamicClass();
MessageBox.Show(dc.Message);
}
//注意:数据文件ClickOnce发布以后位置的变化
private void button3_Click(object sender, EventArgs e)
{
// 会报错
if (File.Exists("UserInfo.xml"))
{
XmlDataDocument doc = new XmlDataDocument();
StreamReader sr = new StreamReader("UserInfo.xml");
doc.DataSet.ReadXml(sr);
tb = doc.DataSet.Tables[0];
this.dataGridView1.DataSource = tb;
sr.Close();
}
}
private void button4_Click(object sender, EventArgs e)
{
XmlDataDocument doc = new XmlDataDocument(tb);
doc.Save("e://UserInfo.xml");
}
}
}
参考:http://blog.joycode.com/ghj/archive/2007/06/14/104220.aspx