小编Mat*_*ing的帖子

锁定文件以进行写入/删除,同时允许任何进程读取

我正在使用C#(.NET)开发应用程序,并且在处理文件锁定时遇到问题.

  • 我的主应用程序(A)需要对某个文件进行读/写访问.
  • 单独的应用程序(B)需要对同一文件的读访问权.
  • 我需要阻止用户在我的应用程序(A)运行时编辑或删除文件.应用程序(A)是长期运行的.在(A)运行时,不得删除该文件,即使没有主动读取或写入该文件也是如此.

我完全控制了(A)和(B)的来源,所以我可以修改它们中的任何一个.

如何在应用程序(A)运行时阻止用户修改/删除文件,同时允许应用程序(A)读/写,应用程序(B)读取?

.net c# file-io

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

WiX安装程序在卸载时删除文件,但在升级时不删除

我有一个安装了WiX安装程序的程序.

程序本身在[CommonAppDataFolder]\[MyAppName] \目录中创建了许多文件.这些文件都具有相同的扩展名(让我们称之为.dat).

升级时,我想保留这些文件.
卸载时,我想删除这些文件.

我目前正在删除这些文件:

<Directory Id='CommonAppDataFolder'>
  <Directory Id='MyCommonAppDataFolder' Name='MyAppName'>
    <Component Id='RemoveFilesComponent' Guid='71cb0cd8-8459-4a8f-89b7-f00977aa7b70'>
      <RemoveFile Id='RemoveFiles' Name='*.dat' On='uninstall'/>
    </Component>
  </Directory>
</Directory>
Run Code Online (Sandbox Code Playgroud)

我有这个促进升级:

<InstallExecuteSequence>
  <RemoveExistingProducts After='InstallInitialize'/>
</InstallExecuteSequence>
Run Code Online (Sandbox Code Playgroud)

现在,当我卸载时,正确删除.dat文件.
但是,当我升级时,.dat文件也会被删除.我想因为升级是在先前版本上执行卸载.

我正确地解决了这个问题吗?如何在升级时保留文件,同时在卸载时删除它们?

windows-installer wix uninstall upgrade delete-file

12
推荐指数
1
解决办法
9371
查看次数

.NET ConcurrentDictionary初始容量设置为MSDN示例文档中的任意素数而不是预期容量.为什么?

我只是在查看ConcurrentDictionaryMSDN文档,我在"示例"代码中看到了这一点:

// We know how many items we want to insert into the ConcurrentDictionary.
// So set the initial capacity to some prime number above that, to ensure that
// the ConcurrentDictionary does not need to be resized while initializing it.
int NUMITEMS = 64;
int initialCapacity = 101;
Run Code Online (Sandbox Code Playgroud)

作为参考,MSDN示例中的字典初始化如下:

ConcurrentDictionary<int, int> cd = new ConcurrentDictionary<int, int>(Environment.ProcessorCount * 2, initialCapacity);
for (int i = 0; i < NUMITEMS; i++) cd[i] = i * i;
Run Code Online (Sandbox Code Playgroud)

在该示例中,字典永远不会包含超过64个项目.为什么不将初始容量设置为64,而不是设置为大于64的看似随意的素数?评论说这是为了确保字典在初始化时不需要调整大小,但为什么需要调整initialCapacity = …

.net c# collections concurrency

11
推荐指数
1
解决办法
3109
查看次数

在 Windows 窗体应用程序中托管 ASP.NET Core API

背景:我正在处理一个涉及 WinForms 应用程序的项目。客户端想要公开一个仅限本地的 HTTP 服务器,以允许其他应用程序通过 REST API(或类似的)在 WinForms 应用程序的运行实例上触发功能。首选是使用 ASP.NET Core 实现上述 API。

因此,我的问题是:如何构建一个项目以在同一进程中同时拥有 ASP.NET Core API 和 WinForms GUI?有什么陷阱我需要警惕吗?

.net c# winforms asp.net-core asp.net-core-webapi

11
推荐指数
1
解决办法
3894
查看次数

.NET ILMerged程序集的性能

我有两个.NET库:"Foo.Bar"和"Foo.Baz".
"Foo.Bar"是自包含的,而"Foo.Baz"引用"Foo.Bar".

假设我执行以下操作:

  • 使用ILMerge将"Foo.Bar.dll"与"Foo.Baz.dll"合并为"Foo1.dll".
  • 创建一个包含"Foo.Bar"和"Foo.Baz"的完整性的新解决方案(因为我可以访问它们的源代码),并将其编译为"Foo2.dll".

在使用外部项目的功能时,Foo1.dll和Foo2.dll的性能是否会有任何差异?如果是这样,这种性能差异有多大,是一次性(负载?)还是持续的差异?这两种方法都有其他优点或缺点吗?

.net performance ilmerge assemblies

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

了解C#编译器如何处理链接linq方法

我正在尝试围绕C#编译器在链接linq方法时所做的事情,特别是在多次链接同一方法时.

简单的例子:假设我试图根据两个条件过滤一系列int.

最明显的事情是这样的:

IEnumerable<int> Method1(IEnumerable<int> input)
{
    return input.Where(i => i % 3 == 0 && i % 5 == 0);
}
Run Code Online (Sandbox Code Playgroud)

但我们可以链接where方法,每个方法都有一个条件:

IEnumerable<int> Method2(IEnumerable<int> input)
{
    return input.Where(i => i % 3 == 0).Where(i => i % 5 == 0);
}
Run Code Online (Sandbox Code Playgroud)

我看了反射器中的IL; 这两种方法明显不同,但目前我不知道进一步分析它:)

我想找出:
a)编译器在每个实例中做什么不同,以及为什么.
b)是否有任何性能影响(不是试图微观优化;只是好奇!)

c# linq cil method-chaining

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

为什么FxCop警告此C#代码中的溢出(CA2233)?

我有以下函数从高字节和低字节获取一个int:

public static int FromBytes(byte high, byte low)
{
    return high * (byte.MaxValue + 1) + low;
}
Run Code Online (Sandbox Code Playgroud)

当我用FxCop分析程序集时,我收到以下严重警告:

CA2233:OperationsShouldNotOverflow
如果没有先验证操作数以防止溢出,则不应进行算术运算.

我无法看到这可能会溢出,所以我只是假设FxCop过于热心.
我错过了什么吗?可以采取哪些措施来纠正我所拥有的(或至少使FxCop警告消失!)?

.net c# fxcop overflow

6
推荐指数
2
解决办法
2460
查看次数

在不同的片段中添加WiX文件的快捷方式

我正在使用heat.exe生成类似于此的片段:

<Fragment>
  <DirectoryRef Id="INSTALLDIR">
    <Component Id="id1" Guid="*">
      <File Id="fid1" KeyPath="yes" Source="SourceDir\Foo1.dll" />
    </Component>
    <Component Id="id2" Guid="*">
      <File Id="fid2" KeyPath="yes" Source="SourceDir\Foo2.dll" />
    </Component>
    <Component Id="id3" Guid="*">
      <File Id="fid3" KeyPath="yes" Source="SourceDir\Bar.exe" />
    </Component>
  </DirectoryRef>
</Fragment>

<Fragment>
    <ComponentGroup Id="Components">
        <ComponentRef Id="id1" />
        <ComponentRef Id="id2" />
        <ComponentRef Id="id3" />
    </ComponentGroup>
</Fragment>
Run Code Online (Sandbox Code Playgroud)

这些片段存储在自动生成的wxs文件中.

然后我将它们添加到我的功能(在主WiX文件中),如下所示:

<ComponentGroupRef Id="Components"/>
Run Code Online (Sandbox Code Playgroud)

这很好用.

但是,我还想在我的开始菜单中添加Bar.exe的快捷方式.理想情况下,我想在我的主wix文件中执行此操作,Bar.exe组件仍驻留在自动生成的wxs文件中.如何在不修改自动生成的代码的情况下解决此问题?

wix shortcut heat

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

以编程方式在Windows中重新启动USB设备

我正在开发的一些软件需要USB设备(我作为SerialPort与USB-to-UART桥接器进行交互).

有时,在计算机从休眠状态重新启动后,设备未被检测到,我无法再通过其串行端口向设备写入或读取设备.必须具有对设备的读/写访问权限.

我不能依赖用户采取任何行动(物理或其他),所以我需要一种方法以编程方式重启设备.

我应该如何使用.NET框架以编程方式在Windows XP/Vista/7中重新启动USB设备?

.net windows usb systems-programming hardware-programming

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

NSubstitute在循环中多次调用时返回意外值

我遇到的是使用NSubstitute模拟具有输出参数的方法时的情况。我不确定如何最好地用文本来解释它,因此我将使用一些人为的示例和测试用例...

在这个人为的示例中,我将使用NSubstitute模拟的IDictionary<string, string>

private static IDictionary<string, string> GetSubstituteDictionary()
{
    IDictionary<string, string> dict = Substitute.For<IDictionary<string, string>>();

    string s;
    dict.TryGetValue("key", out s).Returns(ci => { ci[1] = "value"; return true; });

    return dict;
}
Run Code Online (Sandbox Code Playgroud)

现在,当我以简单的方式使用此模拟对象时,它会按预期返回:

[Test]
public void ExampleOne()
{
    var dict = GetSubstituteDictionary();

    string value;
    bool result = dict.TryGetValue("key", out value);

    Assert.That(result, Is.True); // this assert passes.
    Assert.That(value, Is.EqualTo("value")); // this assert passes.
}
Run Code Online (Sandbox Code Playgroud)

但是,当我在for循环中调用相同的代码时,我得到了一些意外的行为:

[Test]
public void ExampleTwo()
{
    var dict = GetSubstituteDictionary();

    for (int i = 0; i …
Run Code Online (Sandbox Code Playgroud)

c# loops nsubstitute

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

ListViewItem 图像未显示在虚拟 ListView 上

我有一个 System.Windows.Forms.ListView。在正常(非虚拟)操作下,ListViewItems 正确显示其图像。但是,当我尝试将 ListView 转换为虚拟模式时,项目不再显示其图像。其他所有内容(文本、索引等)均按预期显示。

ListView 处于详细信息模式。SmallImageList 设置正确。每个 ListViewItem 都有正确设置的 ImageKey。ImageList 已正确填充。

为什么图像没有显示在虚拟 ListView 上?我该如何确保图像按预期显示?

编辑:请参阅以下代码作为演示我遇到的问题的示例。

static class Program
{
    [STAThread]
    static void Main(string[] args)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new VirtualListViewExample());
    }
}

class VirtualListViewExample : Form
{
    public VirtualListViewExample()
    {
        Image image = new Bitmap(16, 16);
        using (Graphics g = Graphics.FromImage(image))
        {
            g.FillRectangle(Brushes.Black, 0, 0, 16, 16);
        }

        ImageList imageList = new ImageList();
        imageList.ColorDepth = ColorDepth.Depth32Bit;
        imageList.ImageSize = new Size(16, 16);
        imageList.Images.Add("key", image);

        ListView normalList = new ListView();
        normalList.Columns.Add("Column");
        normalList.Dock …
Run Code Online (Sandbox Code Playgroud)

.net c# listview winforms

0
推荐指数
1
解决办法
2249
查看次数