小编nor*_*hov的帖子

如何使用WNetCancelConnection2正确关闭连接?

我想在需要时访问共享并取消访问权限.我使用以下代码:

class Program
{
    const string Share = "\\\\srv\\share";

    static void ListFiles()
    {
        foreach (var dir in Directory.EnumerateDirectories(Share))
            Console.WriteLine(dir);
    }

    static void Main(string[] args)
    {
        using (var connection = new NetworkConnection(Share, new System.Net.NetworkCredential("user", "password")))
            ListFiles();
        ListFiles();
    }
}
Run Code Online (Sandbox Code Playgroud)

第一个ListFiles()调用成功.我希望第二次ListFiles()调用失败但它也会成功.如何正确取消连接?似乎WNetCancelConnection2不起作用.

这也在这里说明.我想知道是否有人能让它发挥作用.

FIY这是一个网络连接类:

public class NetworkConnection : IDisposable
{
    string _networkName;

    public NetworkConnection(string networkName,
        NetworkCredential credentials)
    {
        _networkName = networkName;

        var netResource = new NetResource()
        {
            Scope = ResourceScope.GlobalNetwork,
            ResourceType = ResourceType.Disk,
            DisplayType = ResourceDisplaytype.Share,
            RemoteName = networkName
        };

        var …
Run Code Online (Sandbox Code Playgroud)

c# windows winapi network-programming

7
推荐指数
1
解决办法
2651
查看次数

在.NET中针对未处理的异常编写minidump是否有意义?

看来当我在这样的异常处理程序中时:

AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
Run Code Online (Sandbox Code Playgroud)

或像这样:

Application.Current.DispatcherUnhandledException += Current_DispatcherUnhandledException;
Run Code Online (Sandbox Code Playgroud)

堆栈已经展开,可以调用我的自定义未处理异常处理程序。似乎此时写一个小型转储没有意义,因为堆栈已经展开。如果不取消堆栈,应用程序将无法理解此异常是否未处理。

即使我可以在UnhandledExceptionEventArgs.ExceptionObject中看到堆栈,也无法在应用程序崩溃的确切位置获得minidump。

还有另一种方法吗?

我知道我可以要求系统写一个转储文件,但是我应该是管理员。

更新:

好。我有个主意)如果在FirstChanceException处理程序中我可以向后走,看看是否未处理此异常,那将是很好的。但这应该足够快以进行生产。

.net c# wpf

5
推荐指数
1
解决办法
1025
查看次数

如何在没有控制台的情况下通过cmd.exe启动分离进程?

我想从C#启动一个外部程序来完全分离.我通过pinvoke使用CreateProcess,因为Process.Start不允许我使用DETACHED_PROCESS.此外,我希望此应用程序将其输出重定向到某个文件.

这是示例代码:

            var processInformation = new ProcessUtility.PROCESS_INFORMATION();
            var securityInfo = new ProcessUtility.STARTUPINFO();
            var sa = new ProcessUtility.SECURITY_ATTRIBUTES();
            sa.Length = Marshal.SizeOf(sa); 

            // Create process with no window and totally detached
            var result = ProcessUtility.CreateProcess(Path.Combine(Environment.SystemDirectory, "cmd.exe"), commandLineArguments, ref sa, ref sa, false,
                ProcessUtility.DETACHED_PROCESS, IntPtr.Zero, null, ref securityInfo, out processInformation);
Run Code Online (Sandbox Code Playgroud)
  1. CommandLineArguments是这样的:"/ c Foo.bat> Foo.log 2>&1"一切正常,Foo.bat填充Foo.log.没有其他控制台窗口可见.完善.

  2. CommandLineArguments是这样的:"/ c Foo.exe> Foo.log 2>&1"Foo.exe是.NET控制台应用程序.未填充Foo.log并在可见控制台窗口中启动Foo.exe.奇怪.为什么行为与1不同?

  3. 仅供参考.CommandLineArguments是这样的:"/ c Foo.exe> Foo.log 2>&1"Foo.exe是.NET Windows应用程序.一切正常,但当我从命令提示符启动此应用程序时,我看不到输出,因为没有分配控制台.

我想2.工作与1.相同为什么有区别?

更新:我不想为自己编写Foo.log,因为启动应用程序将被终止.

更新:好的,我编写了一些代码来指定只有一个句柄被继承但是当使用EXTENDED_STARTUPINFO_PRESENT调用时,CreateProcess会给出错误87(即使它存在且为空).

你能帮帮我吗?

public class ProcessUtility
{
    // Process creation flags
    const uint ZERO_FLAG = 0x00000000; …
Run Code Online (Sandbox Code Playgroud)

c# windows cmd batch-file console-application

5
推荐指数
1
解决办法
4213
查看次数

如何在打字稿中使用collections.js?

我想在我的打字稿Angular2项目中使用collections.js.我想从multimap开始.没有@ types/collections包,并且在typings中没有collections.js.

当我写作

const MultiMap = require('collections/multi-map')
Run Code Online (Sandbox Code Playgroud)

我在Agnular2中得到了"NoReviderError for PlatformRef".

typescript angular

5
推荐指数
1
解决办法
1189
查看次数

Autofac 无法解析类型化 HttpClient 的枚举

我有许多服务需要使用来自 HttpClientFactory 的类型化 HttpClient。虽然我可以解析一项服务,但我无法解析该服务的 IEnumerable。

interface IMyHttpClient
{
}

class MyHttpClient: IMyHttpClient
{
    public MyHttpClient(HttpClient client)
    {
    }
}

class Program
{
    static void Main(string[] args)
    {
        var services = new ServiceCollection();
        services.AddHttpClient()
                .AddHttpClient<IMyHttpClient, MyHttpClient>();

        var builder = new ContainerBuilder();
        // Exception goes away when remove this line
        builder.RegisterType<MyHttpClient>().As<IMyHttpClient>();
        builder.Populate(services);
        var provider = builder.Build();

        // ============== This works
        // provider.Resolve<IMyHttpClient>();

        // ============== This throws exception
        provider.Resolve<IEnumerable<IMyHttpClient>>();
    }
}
Run Code Online (Sandbox Code Playgroud)

构造函数将被调用一次,然后抛出异常:

``` DependencyResolutionException:在类型“ConsoleApp2.MyHttpClient”上找不到任何带有“Autofac.Core.Activators.Reflection.DefaultConstructorFinder”的构造函数可以使用可用的服务和参数调用:无法解析参数“System.Net.Http.HttpClient”构造函数 'Void .ctor(System.Net.Http.HttpClient)' 的客户端'。

``

问题是 AddHttpClient 添加了它自己的 IMyHttpClient 注册。但我只想使用 …

autofac httpclientfactory

2
推荐指数
1
解决办法
3305
查看次数