首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

在 WebClient 请求期间发生错误,如何避免进程异常

2013-10-29 
在 WebClient 请求期间发生异常,如何处理进程错误?private static string GetUpdateServerIp(){XmlDocumen

在 WebClient 请求期间发生异常,如何处理进程错误?


private static string GetUpdateServerIp()
        {
            XmlDocument xdRead = new XmlDocument();
            string strAppPath = AppDomain.CurrentDomain.BaseDirectory;
            xdRead.Load(strAppPath + "CourtInteTIM.ElisorEesWhs.config");   //XML地址
            XmlElement xnode = (XmlElement)xdRead.SelectSingleNode("/configuration/UpdateImformation");
            return xnode.GetAttribute("UpdateServerIp");
        }
        private static void DownLoadNewFile(string strFileName)
        {
            WebClient webClient = new WebClient();
            webClient.DownloadFile(@"http://" + GetUpdateServerIp() + "/CourtElisorEesWhs/UpdateVission/" + strFileName, @strFileName);
        }
        private static void ReleaseService()//释放服务和进程
        {
            ServiceController service = new ServiceController("CourtInteTIM  RemotingService");
            service.Stop();
            foreach (Process myProcess in Process.GetProcesses())
            {
                if (myProcess.ProcessName.ToUpper().IndexOf("ElisorEesWhs".ToUpper())!=-1)
                {
                    myProcess.Kill();
                    myProcess.Close();
                }
            }
        }
        private static void OpenService()
        {
            ServiceController service = new ServiceController("CourtInteTIM  RemotingService");
            service.Start();
        }
        private static int GetFileVission(string strFilePath)
        {
            string[] astrVission = (Assembly.LoadFrom(strFilePath).GetName().Version.ToString()).Split('.');
            string strVission = string.Empty;
            for (int i = 0; i < astrVission.Length; i++)
            {
                strVission += astrVission[i].ToString();
            }
            return Convert.ToInt32(strVission);
        }
        public static void UpdateSoftWare()
        {
            DownLoadNewFile("UpdateConfiguration.xml");
            XmlDocument xdRead = new XmlDocument();
            string strAppPath = AppDomain.CurrentDomain.BaseDirectory;
            xdRead.Load(strAppPath + "\\UpdateConfiguration.xml");
            XmlNodeList xnlNode=xdRead.GetElementsByTagName("add");
            for (int i = 0; i < xnlNode.Count; i++)
            {
                 string strUpdateFile=xnlNode[i].Attributes["fileName"].Value;
                 if (File.Exists(strAppPath + strUpdateFile)==true)


                {
                    int nOriginalVission = GetFileVission(strAppPath + strUpdateFile);
                    string[] astrVission = (xnlNode[i].Attributes["version"].Value).Split('.');
                    string strVission = string.Empty;
                    for (int j = 0; j < astrVission.Length; j++)
                    {
                        strVission += astrVission[j].ToString();
                    }
                    int nUpdateVission = Convert.ToInt32(strVission);
                    if (nOriginalVission != nUpdateVission)
                    {
                        ReleaseService();
                        DownLoadNewFile(strUpdateFile);
                        //OpenService();
                    } 
                } 
                else
                {
                    ReleaseService();
                    DownLoadNewFile(strUpdateFile);
                    //OpenService();
                }
            }
        }




错误提示:
System.Net.WebException: 在 WebClient 请求期间发生异常。 ---> System.IO.IOException: 文件“C:\Documents and Settings\Administrator\桌面\解决方案测试\审判数据采集与挖掘\bin\Release\CourtInteTIM.AssistantFunctionLibrary.dll”正由另一进程使用,因此该进程无法访问此文件。
   在 System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   在 System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath)
   在 System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
   在 System.Net.WebClient.DownloadFile(Uri address, String fileName)
   --- 内部异常堆栈跟踪的结尾 ---
   在 System.Net.WebClient.DownloadFile(Uri address, String fileName)
   在 采集挖掘更新.Update.DownLoadNewFile(String strFileName) 位置 C:\Documents and Settings\Administrator\桌面\解决方案测试\审判数据采集与挖掘\采集挖掘更新\Update.cs:行号 65
   在 采集挖掘更新.Update.UpdateSoftWare() 位置 C:\Documents and Settings\Administrator\桌面\解决方案测试\审判数据采集与挖掘\采集挖掘更新\Update.cs:行号 166
   在 采集挖掘更新.Update.Update_Load(Object sender, EventArgs e) 位置 C:\Documents and Settings\Administrator\桌面\解决方案测试\审判数据采集与挖掘\采集挖掘更新\Update.cs:行号 28
   在 System.Windows.Forms.Form.OnLoad(EventArgs e)
   在 System.Windows.Forms.Form.OnCreateControl()
   在 System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
   在 System.Windows.Forms.Control.CreateControl()
   在 System.Windows.Forms.Control.WmShowWindow(Message& m)
   在 System.Windows.Forms.Control.WndProc(Message& m)
   在 System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   在 System.Windows.Forms.Form.WmShowWindow(Message& m)
   在 System.Windows.Forms.Form.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)


************** 已加载的程序集 **************
mscorlib
    程序集版本: 4.0.0.0
    Win32 版本: 4.0.30319.1008 (RTMGDR.030319-1000)
    基本代码: file:///C:/WINDOWS/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
采集挖掘更新
    程序集版本: 1.0.0.0
    Win32 版本: 1.0.0.0
    基本代码: file:///C:/Documents%20and%20Settings/Administrator/桌面/解决方案测试/审判数据采集与挖掘/bin/Release/采集挖掘更新.exe
----------------------------------------
System.Windows.Forms
    程序集版本: 4.0.0.0
    Win32 版本: 4.0.30319.1002 built by: RTMGDR
    基本代码: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
    程序集版本: 4.0.0.0
    Win32 版本: 4.0.30319.1001 built by: RTMGDR
    基本代码: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System
    程序集版本: 4.0.0.0
    Win32 版本: 4.0.30319.1001 built by: RTMGDR
    基本代码: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Xml
    程序集版本: 4.0.0.0
    Win32 版本: 4.0.30319.1015 built by: RTMGDR
    基本代码: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
System.Configuration
    程序集版本: 4.0.0.0
    Win32 版本: 4.0.30319.1015 (RTMGDR.030319-1000)
    基本代码: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Configuration/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Configuration.dll
----------------------------------------
mscorlib.resources
    程序集版本: 4.0.0.0
    Win32 版本: 4.0.30319.1 (RTMRel.030319-0100)
    基本代码: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/mscorlib.resources/v4.0_4.0.0.0_zh-Hans_b77a5c561934e089/mscorlib.resources.dll
----------------------------------------
CourtInteTIM.AssistantFunctionLibrary
    程序集版本: 1.0.0.0
    Win32 版本: 1.0.0.0
    基本代码: file:///C:/Documents%20and%20Settings/Administrator/桌面/解决方案测试/审判数据采集与挖掘/bin/Release/CourtInteTIM.AssistantFunctionLibrary.DLL
----------------------------------------
System.resources
    程序集版本: 4.0.0.0
    Win32 版本: 4.0.30319.1 built by: RTMRel
    基本代码: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.resources/v4.0_4.0.0.0_zh-Hans_b77a5c561934e089/System.resources.dll
----------------------------------------
System.Windows.Forms.resources
    程序集版本: 4.0.0.0
    Win32 版本: 4.0.30319.1 built by: RTMRel
    基本代码: file:///C:/WINDOWS/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms.resources/v4.0_4.0.0.0_zh-Hans_b77a5c561934e089/System.Windows.Forms.resources.dll
----------------------------------------

************** JIT 调试 **************
要启用实时(JIT)调试,
该应用程序或计算机的 .config 文件(machine.config)的 system.windows.forms 节中必须设置
jitDebugging 值。
编译应用程序时还必须启用
调试。

例如: 

<configuration>
    <system.windows.forms jitDebugging="true" />
</configuration> 异常 程序更新 进程 C# webClient
[解决办法]
每次操作完物理文件后,先关闭。

操作XML也一样,最后需要保存。


[解决办法]
文件被打开了,读完之后close()下,如果还有问题一定是其他程序在用当前的文件

热点排行