Tag: C#的

如何获得CPU温度?

我需要为我正在开发的应用程序收集一些系统信息。 可用内存和CPU负载很容易使用C#。 不幸的是,CPU温度并不那么容易。 我曾尝试使用WMI,但我无法使用任何东西 Win32_TemperatureProbe 要么 MSAcpi_ThermalZoneTemperature 有没有人处理过这个问题? 我想知道如何监测程序,如SiSoftware桑德拉,可以得到这些信息… 以防有人感兴趣,下面是这个类的代码: public class SystemInformation { private System.Diagnostics.PerformanceCounter m_memoryCounter; private System.Diagnostics.PerformanceCounter m_CPUCounter; public SystemInformation() { m_memoryCounter = new System.Diagnostics.PerformanceCounter(); m_memoryCounter.CategoryName = "Memory"; m_memoryCounter.CounterName = "Available MBytes"; m_CPUCounter = new System.Diagnostics.PerformanceCounter(); m_CPUCounter.CategoryName = "Processor"; m_CPUCounter.CounterName = "% Processor Time"; m_CPUCounter.InstanceName = "_Total"; } public float GetAvailableMemory() { return […]

多值词典

我将如何在C#中创build一个多值字典? 例如Dictionary<T,T,T> ,其中第一个T是关键字,另外两个是值。 所以这将是可能的: Dictionary<int,object,double> 谢谢

你可以在哪里,不能在C中声明新的variables?

我听到(可能来自一位老师)应该在程序/函数之上声明所有的variables,并且在声明中声明新的variables可能会导致问题。 但是当时我正在读K&R,而且我遇到了这样一句话:“variables的声明(包括初始化)可以在左括号之后,引入任何复合语句,而不仅仅是开始函数的语句”。 他举了一个例子: if (n > 0){ int i; for (i=0;i<n;i++) … } 我对这个概念起了一点作用,即使对于数组也是如此。 例如: int main(){ int x = 0 ; while (x<10){ if (x>5){ int y[x]; y[0] = 10; printf("%d %d\n",y[0],y[4]); } x++; } } 那么到底什么时候我不允许声明variables呢? 例如,如果我的variables声明不正确的后面大括号? 像这样: int main(){ int x = 10; x++; printf("%d\n",x); int z = 6; printf("%d\n",z); } 这可能会造成麻烦取决于程序/机器?

在ItemsControl DataTemplate中设置Canvas属性

我试图绑定到这个ItemsControl : <ItemsControl ItemsSource="{Binding Path=Nodes, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"> <ItemsControl.ItemsPanel> <ItemsPanelTemplate> <Canvas /> </ItemsPanelTemplate> </ItemsControl.ItemsPanel> </ItemsControl> 通过使用这个DataTemplate ,我试图单独地将我的Node元素放置在Canvas : <DataTemplate DataType="{x:Type Model:EndNode}"> <Controls:EndNodeControl Canvas.Left="{Binding Path=XPos}" Canvas.Top="{Binding Path=YPos}" /> </DataTemplate> 但是,它没有按预期工作。 我所有的节点元素都在同一个位置上绘制。 有关如何完成此任何build议?

如何在C ++中使用基类的构造函数和赋值运算符?

我有一组构造函数和一个赋值运算符。 class B { public: B(); B(const string & s); B(const B & b){(*this) = b;}; B & operator= (const B & b); private: virtual void foo(); // and other private member variables and functions } 我想创build一个只会覆盖函数foo()的inheritance类D,不需要其他更改。 但我想要D具有相同的一组构造函数,包括复制构造函数和赋值运算符为B: D(const D & d){(*this) = d;}; D & operator= (const D & d); 我是否必须在D中重写它们,还是有办法使用B的构造函数和操作符? 我特别想避免重写赋值运算符,因为它必须访问所有B的私有成员variables。

snprintf和Visual Studio 2010

我不幸被一个项目的VS 2010卡住了,并且注意到下面的代码仍然不能使用不符合标准的编译器来编译: #include <stdio.h> #include <stdlib.h> int main (void) { char buffer[512]; snprintf(buffer, sizeof(buffer), "SomeString"); return 0; } (编译失败,错误:C3861:'snprintf':标识符未find) 我记得在VS2005的情况下,我很震惊地看到它仍然没有被修复。 有谁知道微软是否有计划将其标准C库移植到2010年?

复制构造函数与Clone()

在C#中,向类中添加(深层)复制function的首选方法是什么? 应该实现复制构造函数,还是从ICloneable派生并实现Clone()方法? 备注 :我在括号内写了“深”,因为我认为这是无关紧要的。 显然其他人不同意,所以我问复制构造函数/操作符/函数是否需要清楚它实现了哪个拷贝变体 。

用正则expression式replace一些组

假设我有以下的正则expression式: -(\d+)- 我想使用C#将组1 (\d+)replace为AA ,以获得: -AA- 现在我正在使用它来replace它: var text = "example-123-example"; var pattern = @"-(\d+)-"; var replaced = Regex.Replace(text, pattern, "-AA-"); 但我不太喜欢这个,因为如果我改变模式来匹配_(\d+)_ ,我将不得不通过_AA_改变replacestring,这是违背DRY原则的。 我正在寻找像这样的东西: 保持匹配的文本到底是怎样的,但是通过this text改变组1,通过another text改变组2。 编辑: 那只是一个例子。 我只是寻找一个通用的方式来做我上面说的。 它应该适用于: anything(\d+)more_text和任何可以想象的模式。 我想要做的只是replace组,并保持比赛的其余部分。

如何在C / C ++中读/写任意位

假设我有一个二进制值为11111111的字节b 我如何读取从第二位开始的3位整数值或从第五位开始写入四位整数值?

HTML敏捷包条带标签不在白名单中

我试图创build一个函数,删除不在白名单中的HTML标签和属性。 我有以下的HTML: <b>first text </b> <b>second text here <a>some text here</a> <a>some text here</a> </b> <a>some twxt here</a> 我正在使用HTML敏捷包,我到目前为止的代码是: static List<string> WhiteNodeList = new List<string> { "b" }; static List<string> WhiteAttrList = new List<string> { }; static HtmlNode htmlNode; public static void RemoveNotInWhiteList(out string _output, HtmlNode pNode, List<string> pWhiteList, List<string> attrWhiteList) { // remove all […]