我写了一个Winform  TCP通讯+ 多线程 程序  前两天 都还好好的, 今天稍作修改然后打包成安装文件就不行了。提示的错误是
对路径“C:\Program Files\微软中国\Server”的访问被拒绝
因为我前两天也试过安装 一切正常,所哟我想不该是文件夹权限问题吧。
我上网看到说好像多线程之间线程对线程的操作会出现问题。不知道是不是。
我这两天的修改中就添加了一个线程对另外一个线程的关闭操作。现在将大概操作代码复制出来        /// <summary>
        /// 关闭某一条连接线程
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public bool CloseClient(int id)
        {
            try
            {
                if (clientTread[id] == null || clientTread[id].sock == null)
                {
                    return true;
                }
                clientTread[id].sock.Close();
                clientTread[id].sock = null;
                clientTread[id] = null;
            }
            catch (Exception e)
            {
                 //e.ToString
            }
            return true;
        }异常信息如下:
有关调用实时(JIT)调试而不是此对话框的详细信息,
请参见此消息的结尾。************** 异常文本 **************
System.UnauthorizedAccessException: 对路径“C:\Program Files\微软中国\Server”的访问被拒绝。
   在 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)
   在 System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize)
   在 System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials)
   在 System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn)
   在 System.Xml.XmlTextReaderImpl.OpenUrlDelegate(Object xmlResolver)
   在 System.Threading.CompressedStack.runTryCode(Object userData)
   在 System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
   在 System.Threading.CompressedStack.Run(CompressedStack compressedStack, ContextCallback callback, Object state)
   在 System.Xml.XmlTextReaderImpl.OpenUrl()
   在 System.Xml.XmlTextReaderImpl.Read()
   在 System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
   在 System.Xml.XmlDocument.Load(XmlReader reader)
   在 System.Xml.XmlDocument.Load(String filename)
   在 TCPServer.XmlManager.RemoveXmlMessage()
   在 TCPServer.Form1.btnStartListenPort_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)
************** 已加载的程序集 **************
mscorlib
    程序集版本: 2.0.0.0
    Win32 版本: 2.0.50727.3082 (QFE.050727-3000)
    基本代码: file:///C:/windows/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
TCPServer
    程序集版本: 1.0.0.0
    Win32 版本: 1.0.0.0
    基本代码: file:///C:/Program%20Files/微软中国/Server/TCPServer.exe
----------------------------------------
System.Windows.Forms
    程序集版本: 2.0.0.0
    Win32 版本: 2.0.50727.3053 (netfxsp.050727-3000)
    基本代码: file:///C:/windows/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
    程序集版本: 2.0.0.0
    Win32 版本: 2.0.50727.3053 (netfxsp.050727-3000)
    基本代码: file:///C:/windows/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
    程序集版本: 2.0.0.0
    Win32 版本: 2.0.50727.3053 (netfxsp.050727-3000)
    基本代码: file:///C:/windows/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System.Web
    程序集版本: 2.0.0.0
    Win32 版本: 2.0.50727.3082 (QFE.050727-3000)
    基本代码: file:///C:/windows/assembly/GAC_32/System.Web/2.0.0.0__b03f5f7f11d50a3a/System.Web.dll
----------------------------------------
System.Xml
    程序集版本: 2.0.0.0
    Win32 版本: 2.0.50727.3082 (QFE.050727-3000)
    基本代码: file:///C:/windows/assembly/GAC_MSIL/System.Xml/2.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------
mscorlib.resources
    程序集版本: 2.0.0.0
    Win32 版本: 2.0.50727.3082 (QFE.050727-3000)
    基本代码: file:///C:/windows/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
System.Windows.Forms.resources
    程序集版本: 2.0.0.0
    Win32 版本: 2.0.50727.1433 (REDBITS.050727-1400)
    基本代码: file:///C:/windows/assembly/GAC_MSIL/System.Windows.Forms.resources/2.0.0.0_zh-CHS_b77a5c561934e089/System.Windows.Forms.resources.dll
----------------------------------------************** JIT 调试 **************
要启用实时(JIT)调试,
该应用程序或计算机的 .config 文件(machine.config)的 system.windows.forms 节中必须设置
jitDebugging 值。
编译应用程序时还必须启用
调试。例如: <configuration>
    <system.windows.forms jitDebugging="true" />
</configuration>启用 JIT 调试后,任何无法处理的异常
都将被发送到在此计算机上注册的 JIT 调试器,
而不是由此对话框处理。

解决方案 »

  1.   

    感谢两位的热心回复。问题已经被我自己找到了。 是由于一个XML引起的,程序开始的时候 我使用了一个清空所有储存在XML中信息的方法
            private string XmlPath ;//System.Windows.Forms.Application.LocalUserAppDataPath;
            public XmlManager()
            {
                XmlPath = System.Windows.Forms.Application.StartupPath;
                XmlPath = XmlPath + @"\XMLFile_TcpServer.xml";
                //XmlPath = XmlPath.Replace(@"\bin\Release", @"\XMLFile_TcpServer.xml");
                //XmlPath = XmlPath.Replace(@"\bin\Debug", @"\XMLFile_TcpServer.xml");        }
            /// <summary>
            /// 清除所有XML信息
            /// </summary>
            public void RemoveXmlMessage()
            {
                try
                {
                    XmlDocument xDoc = new XmlDocument();
                    xDoc.Load(XmlPath);
                    XmlNode xNode;
                    xNode = xDoc.SelectSingleNode("//appSettings");
                    xNode.RemoveAll();
                    xDoc.Save(XmlPath);
                }
                catch (Exception e)
                {
                    LogManager.WriteLog(LogFile.Error, "清空所有XML信息出错:"+e.Message+" "+XmlPath);
                }
            }其中的 XmlPath 在调试的时候是在Dubug 或者 Release 目录中没有错, 但是安装以后就出现问题了。 在路径“C:\Program Files\微软中国\Server” 下找不到这个XML文件,所以在执行删除XML 中出现了错误, 而开始我没有try起这个异常, 所以没有捕捉到。   
    解决办法:在生成安装包的时候 添加那个XML文件,然后将上面的XmlPath 使用
    XmlPath = System.Windows.Forms.Application.StartupPath;
                XmlPath = XmlPath + @"\XMLFile_TcpServer.xml";
    就可以了