c#-3.0

WMI Files Montioring using C#

主宰稳场 提交于 2020-01-05 12:16:25
问题 I am working on WMI(Windows Management Instrumentation) in C# and stuck at a point. I have to create an application using WMI (C#) similar to File System Watcher. I would like to get notified every time whenever within a particular folder a new file is created or deleted. MY WQL query is : SELECT * from _InstanceModificationEvent within 2 where TargetInstance ISA 'CIM_DataFile' and TargetInstance.Drive = 'C:' AND TargetInstance.Path='\\Test' While running the query using wbemtest , it

How to make class truly Immutable? [duplicate]

那年仲夏 提交于 2020-01-05 10:16:13
问题 This question already has answers here : How do I create an immutable Class? (6 answers) Closed 6 years ago . I just looked into IReadOnlyList<T> to create readonly list. But I think it is not 100% readonly. I can not add/remove the item from the list but I can still modify the members. Consider this example. class Program { static void Main(string[] args) { List<Test> list = new List<Test>(); list.Add(new Test() { MyProperty = 10 }); list.Add(new Test() { MyProperty = 20 }); IReadOnlyList

Convert List to Double Array by LINQ (C#3.0)

一个人想着一个人 提交于 2020-01-05 07:10:36
问题 I have two lists x1 and x2 of type double List<double> x1 = new List<double> { 0.0330, -0.6463}; List<double> x2 = new List<double> { -0.2718, -0.2240}; I am using the below function to convert it to double array List<List<double>> xData = new List<List<double>> { x1, x2 }; double[,] xArray = new double[xData.Count, xData[0].Count]; for (int i = 0; i < xData.Count; i++) { for (int j = 0; j < xData[0].Count; j++) { xArray[i, j] = xData[i][j]; } } Is it possible to do the same stuff (i.e. the

Linq to sql truncating string returned by Stored Procedure

淺唱寂寞╮ 提交于 2020-01-05 05:42:11
问题 I have asked this question before. but i was not able to get any answer. may be i wasnt very clear. let me give some more details. I have a SP which returns a long string. here is dbml file code [Function(Name="dbo.spX")] public ISingleResult<spXResult> spX([Parameter(DbType="VarChar(8000)")] string str) { IExecuteResult result = this.ExecuteMethodCall(this, ((MethodInfo)(MethodInfo.GetCurrentMethod())), str); return ((ISingleResult<spXResult>)(result.ReturnValue)); } and here is spXResult

Executing batch file with ProcessStartInfo

和自甴很熟 提交于 2020-01-05 04:58:10
问题 I am using ProcessStartInfo class to execute a batch file. But I am not so aware of using it properly. When I execute a batch file can we track is there any error or not. In my scenario, batch will start another process and it is successfully started it will be keep on executing. And if there is any error executing batch file it will exit. This is the code I am using. I have to track both successfull start and failure as well. internal bool ExecuteBatchFile(string fileName, int executionTime)

Using C# xmlserializer on derived classes while controlling the generated XML element

为君一笑 提交于 2020-01-04 09:09:22
问题 I've got a C# structure of classes with an abstract base class and various classes derived from this abstract class. [System.Xml.Serialization.XmlInclude(typeof(B))] public abstract class A { ... } [Serializable] [XmlType("typename")] public class B : A { ... } Furthermore, I'm using an instance of class B within another class C as a field with its abstract type like this: public class C { ... public A myItem { get; set; } //A is actually of type B ... } Now, when I serialize my class C via

Using C# xmlserializer on derived classes while controlling the generated XML element

折月煮酒 提交于 2020-01-04 09:09:11
问题 I've got a C# structure of classes with an abstract base class and various classes derived from this abstract class. [System.Xml.Serialization.XmlInclude(typeof(B))] public abstract class A { ... } [Serializable] [XmlType("typename")] public class B : A { ... } Furthermore, I'm using an instance of class B within another class C as a field with its abstract type like this: public class C { ... public A myItem { get; set; } //A is actually of type B ... } Now, when I serialize my class C via

Topshelf exception

左心房为你撑大大i 提交于 2020-01-04 04:32:46
问题 I'm using topshelf and I'm getting this exception when I try to use the "-i" option to install as a service. Unable to cast object of type 'Magnum.CommandLineParser.SwitchElement' to type 'Magnum.CommandLineParser.IArgumentElement'. Exception occurs in this function static void Set(TopshelfArguments args, IEnumerable<ICommandLineElement> commandLineElements) { var command = commandLineElements .Take(1) .Select(x => (IArgumentElement) x) //EXCEPTION BREAKS ON THIS LINE .Select(x => x.Id)

Tell LINQ Distinct which item to return

落花浮王杯 提交于 2020-01-04 02:34:25
问题 I understand how to do a Distinct() on a IEnumerable and that I have to create an IEqualityComparer for more advanced stuff however is there a way in which you can tell which duplicated item to return? For example say you have a List<T> List<MyClass> test = new List<MyClass>(); test.Add(new MyClass {ID = 1, InnerID = 4}); test.Add(new MyClass {ID = 2, InnerID = 4}); test.Add(new MyClass {ID = 3, InnerID = 14}); test.Add(new MyClass {ID = 4, InnerID = 14}); You then do: var distinctItems =

How can I wait for a thread to finish running other than by calling thread.Join()?

泪湿孤枕 提交于 2020-01-03 20:48:48
问题 I am working on a project where the methods are called from the an application which is available with the library. I have the below code in my button click event: Thread thread = new Thread(new ThreadStart(AddPics)); thread.Priority = ThreadPriority.Highest; thread.Start(); execute(); But while running the sample always the execution point moves to the execute. How to make the program to execute the AddPics method first. I have tried thread.Join() and it worked, but I want to know if there