使用模拟后释放网络资源C# [英] releasing network resources after use of impersonisation C#

查看:142
本文介绍了使用模拟后释放网络资源C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#.net 4.5当应用程序连接到共享路径并复制文件时。这是使用模仿完成的。但是在运行一次代码之后,我可以看到连接对该服务器是活跃的,尽管没有数据被传输。注意:我正在使用资源监视器来观察。

如何在任务完成后高效关闭连接/停止进程。

同样我需要实现用于管理远程的Windows服务和任务调度程序。

应用程序(现在控制台应用程序)按顺序连接到多个服务器,因此需要立即清除内存。



下面是代码片段:

  string  fileName =   test.zip; 
使用(Impersonation.LogonUser( domainname) 用户名 password,LogonType.NewCredentials))
{
File.Copy( \\shareddrivepath \+ fileName,Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+ \\ + fileName,

}



Impersonation类来自 https://github.com/mj1856/SimpleImpersonation



我更改了 http:// msdn .microsoft.co m / en-us / library / w070t6ka(v = vs.110).aspx



请建议如何关闭处于打开状态的连接状态,此进程正在运行svchost.exe

解决方案

 WNetCancelConnection2 



as idle63说。


I am using C# .net 4.5 While the application is connecting to a shared path and copying files. This is done using impersonisation. But after running once the code, I can see that the connection is alive to that server, although no data is being transferred. Note: I am using Resource Monitor to observe.
How do I efficiently close the connection/stop the process after the task is done.
Also the same I need to implement for managing windows services and task schedulers for remote.
The application (right now console app) sequentially makes multiple connection to multiple server, hence need to clear the memory immediately.

Below is the code snippet:

string fileName = "test.zip";
            using(Impersonation.LogonUser("domainname","username","password",LogonType.NewCredentials))
            {
                File.Copy("\\shareddrivepath\"+ fileName, Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\" + fileName, 

     }


The Impersonation class is from https://github.com/mj1856/SimpleImpersonation

I had change the implementation as shown on http://msdn.microsoft.com/en-us/library/w070t6ka(v=vs.110).aspx

Please suggest how do I close the connection which is in open state, this run in process svchost.exe

解决方案

WNetCancelConnection2


As idle63 said.


这篇关于使用模拟后释放网络资源C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆