我正在使用C#(.NET)开发应用程序,并且在处理文件锁定时遇到问题.
我完全控制了(A)和(B)的来源,所以我可以修改它们中的任何一个.
如何在应用程序(A)运行时阻止用户修改/删除文件,同时允许应用程序(A)读/写,应用程序(B)读取?
我有一个安装了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文件也会被删除.我想因为升级是在先前版本上执行卸载.
我正确地解决了这个问题吗?如何在升级时保留文件,同时在卸载时删除它们?
我只是在查看ConcurrentDictionary的MSDN文档,我在"示例"代码中看到了这一点:
// 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 = …
背景:我正在处理一个涉及 WinForms 应用程序的项目。客户端想要公开一个仅限本地的 HTTP 服务器,以允许其他应用程序通过 REST API(或类似的)在 WinForms 应用程序的运行实例上触发功能。首选是使用 ASP.NET Core 实现上述 API。
因此,我的问题是:如何构建一个项目以在同一进程中同时拥有 ASP.NET Core API 和 WinForms GUI?有什么陷阱我需要警惕吗?
我有两个.NET库:"Foo.Bar"和"Foo.Baz".
"Foo.Bar"是自包含的,而"Foo.Baz"引用"Foo.Bar".
假设我执行以下操作:
在使用外部项目的功能时,Foo1.dll和Foo2.dll的性能是否会有任何差异?如果是这样,这种性能差异有多大,是一次性(负载?)还是持续的差异?这两种方法都有其他优点或缺点吗?
我正在尝试围绕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)是否有任何性能影响(不是试图微观优化;只是好奇!)
我有以下函数从高字节和低字节获取一个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警告消失!)?
我正在使用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文件中.如何在不修改自动生成的代码的情况下解决此问题?
我正在开发的一些软件需要USB设备(我作为SerialPort与USB-to-UART桥接器进行交互).
有时,在计算机从休眠状态重新启动后,设备未被检测到,我无法再通过其串行端口向设备写入或读取设备.必须具有对设备的读/写访问权限.
我不能依赖用户采取任何行动(物理或其他),所以我需要一种方法以编程方式重启设备.
我应该如何使用.NET框架以编程方式在Windows XP/Vista/7中重新启动USB设备?
我遇到的是使用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) 我有一个 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 ×7
c# ×7
winforms ×2
wix ×2
asp.net-core ×1
assemblies ×1
cil ×1
collections ×1
concurrency ×1
delete-file ×1
file-io ×1
fxcop ×1
heat ×1
ilmerge ×1
linq ×1
listview ×1
loops ×1
nsubstitute ×1
overflow ×1
performance ×1
shortcut ×1
uninstall ×1
upgrade ×1
usb ×1
windows ×1