//更新xml数据
private void button1_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
try
{
button1.Enabled = false; ds.ReadXml(xmllist + @"/filelist.xml");
foreach (DataRow row in ds.Tables[0].Rows)
{
string s = row["name"].ToString();
DownFile(Application.StartupPath + @"\xml\" + s, xmllist + s, s);
}
label2.Text = "更新完成";
button1.Enabled = true;
treeView1.Nodes.Clear(); //更新后重新加载文件
fandxml();
}
catch
{
button1.Enabled = true;
label2.Text = "更新失败!";
MessageBox.Show("连接更新文件失败,请检查网络或稍后再试!", "连接失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
这个地方是去网站上下载xml文件,在调试的时候一点问题没有,但是发布程序以后,我在本机安装测试,发现更新不了,一直提示更新失败,弹出那个失败对话框。网络应该是没问题的,不知道是哪里问题,请问要怎么跟踪调试以找出问题所在
类似 :System.Reflection.Assembly Asm = System.Reflection.Assembly.GetExecutingAssembly();
路径=new FileInfo(Asm.Location).Directory + "\filelist.xml";
谢谢,大概知道是什么问题了,但还不是很明白。程序是这样运行的:先打开服务器上的一个xml文件(filelist.xml)读取其中内容,这个文件里有其他xml文件名字,读取到名字以后,调用文件下载函数将这个xml文件下回本地。
难道是下载路径出问题了吗?我把try去掉看看出什么错先
请参见此消息的结尾。************** 异常文本 **************
System.UnauthorizedAccessException: 对路径“C:\Program Files\hbplayer\xml\3_lz.xml”的访问被拒绝。 在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
在 System.IO.File.Delete(String path)
在 maxplayer.Form1.DownFile(String FilePath, String StrUri, String s)
在 maxplayer.Form1.button1_Click(Object sender, EventArgs e)
在 System.Windows.Forms.Control.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnClick(EventArgs e)
在 System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
在 System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.ButtonBase.WndProc(Message& m)
在 System.Windows.Forms.Button.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
private void button1_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
//try
//{
button1.Enabled = false; ds.ReadXml(xmllist + @"/filelist.xml");
foreach (DataRow row in ds.Tables[0].Rows)
{
string s = row["name"].ToString();
DownFile(Application.StartupPath + @"\xml\" + s, xmllist + s, s);
}
label2.Text = "更新完成";
button1.Enabled = true;
treeView1.Nodes.Clear(); //更新后重新加载文件
fandxml();
//}
//catch
//{
//button1.Enabled = true;
//label2.Text = "更新失败!";
// MessageBox.Show("连接更新文件失败,请检查网络或稍后再试!", "连接失败", MessageBoxButtons.OK, MessageBoxIcon.Error);
//}
}///更下载文件
/// <param name="FilePath">本地路径</param>
/// <param name="StrUri">远程地址</param>
public void DownFile(string FilePath, string StrUri, string s)
{
File.Delete(FilePath);//先删后建立
System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(StrUri);
System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)Myrq.GetResponse();
long totalBytes = myrp.ContentLength;
System.IO.Stream st = myrp.GetResponseStream();
System.IO.Stream so = new System.IO.FileStream(FilePath, System.IO.FileMode.Append, System.IO.FileAccess.Write, System.IO.FileShare.Write);
long totalDownloadedByte = 0;
byte[] by = new byte[1024];
int osize = st.Read(by, 0, (int)by.Length);
while (osize > 0)
{
totalDownloadedByte = osize + totalDownloadedByte;
Application.DoEvents();
so.Write(by, 0, osize);
osize = st.Read(by, 0, (int)by.Length);
}
so.Close();
st.Close();
label2.Text = "文件" + s + "下载中...";
}