c#-2.0

Trying to cast a boxed int to byte

风流意气都作罢 提交于 2019-12-19 17:39:41
问题 Code to illustrate : int i = 5; object obj = i; byte b = (byte)obj; // X When run, this generates a System.InvalidCastException ("Specified cast is not valid") at line "X". Doing a double cast works : byte b = (byte)(int)obj; I would have thought that you ought to be able to cast a boxed int (if it has a value in the range 0..255) to a byte. Can anyone shed any light on this ? (This is in .net 2.0, in case that matters). 回答1: The difference in behaviour you're seeing is the difference between

How to read TermainsServices IADsTSUserEx Property from LDAP in C#?

风流意气都作罢 提交于 2019-12-19 10:24:06
问题 I have read the following properties from AD, TerminalServicesProfilePath TerminalServicesHomeDirectory TerminalServicesHomeDrive I've tried DirectoryEntry and DirectorySearcher. But they does not include the properties. I found some example in vbscript and VC to read them. However I failed to make it working in C#. Am I missing some tricky thing? EDIT: Am I have to run it on "Windows Server" to make it works? Can it be read from win XP? 回答1: I don't remember exactly, but it's something like

Accessing deleted rows from a DataTable

房东的猫 提交于 2019-12-19 02:08:43
问题 I have a parent WinForm that has a MyDataTable _dt as a member. The MyDataTable type was created in the "typed dataset" designer tool in Visual Studio 2005 (MyDataTable inherits from DataTable) _dt gets populated from a db via ADO.NET. Based on changes from user interaction in the form, I delete a row from the table like so: _dt.FindBySomeKey(_someKey).Delete(); Later on, _dt is passed by value to a dialog form. From there, I need to scan through all the rows to build a string: foreach

Get report from jasperserver using REST webservice and asp.net C#

*爱你&永不变心* 提交于 2019-12-18 11:55:18
问题 You can use the jasperservers webservices (SOAP and REST is available) to get mange and run reports on from a web application. The SOAP wsdl is not compatible with asp.net c# (at least, I cannot get it to work), so I decided to use the REST webservice. I am ALMOST there, but I can't retrieve the report itself. does anyone know what goes wrong? I am using jasperserver CE 4.5 on Linux. // Setup WebClient WebClient httpclient = new WebClient(); //Basic Auth httpclient.Credentials = new

Get domain name of a url in C# / .NET [duplicate]

若如初见. 提交于 2019-12-18 05:43:25
问题 This question already has answers here : Top level domain from URL in C# (7 answers) Closed 6 years ago . The code: string sURL = "http://subdomain.website.com/index.htm"; MessageBox.Show(new System.Uri(sURL).Host); gives me "subdomain.website.com" But I need the main domain "website.com" for any url or web link. How do I do that? 回答1: You can do this to get just the last two segments of the host name: string[] hostParts = new System.Uri(sURL).Host.Split('.'); string domain = String.Join(".",

Get domain name of a url in C# / .NET [duplicate]

天涯浪子 提交于 2019-12-18 05:43:17
问题 This question already has answers here : Top level domain from URL in C# (7 answers) Closed 6 years ago . The code: string sURL = "http://subdomain.website.com/index.htm"; MessageBox.Show(new System.Uri(sURL).Host); gives me "subdomain.website.com" But I need the main domain "website.com" for any url or web link. How do I do that? 回答1: You can do this to get just the last two segments of the host name: string[] hostParts = new System.Uri(sURL).Host.Split('.'); string domain = String.Join(".",

Restrict custom attribute so that it can be applied only to Specific types in C#?

陌路散爱 提交于 2019-12-17 19:28:01
问题 I have a custom attribute which is applied to class properties and the class itself. Now all the classes that must apply my custom attribute are derived from a single base class. How can I restrict my Custom Attribute so that it can be applied to only those classes, that must derive from my base class? How do I do this? 回答1: Darn, I hate it when I prove myself wrong... it works if you define the attribute as a protected nested type of the base-class: abstract class MyBase { [AttributeUsage

Unable to cast COM object - Microsoft outlook & C#

强颜欢笑 提交于 2019-12-17 18:28:11
问题 I have written this code to view the unread items in my outlook mail box and here is the code: Microsoft.Office.Interop.Outlook.Application app; Microsoft.Office.Interop.Outlook.Items items; Microsoft.Office.Interop.Outlook.NameSpace ns; Microsoft.Office.Interop.Outlook.MAPIFolder inbox; Microsoft.Office.Interop.Outlook.Application application = new Microsoft.Office.Interop.Outlook.Application(); app = application; ns = application.Session; inbox = ns.GetDefaultFolder(Microsoft.Office.Interop

C# ?: Conditional Operator

拜拜、爱过 提交于 2019-12-17 15:46:37
问题 I have this extract of C# 2.0 source code: object valueFromDatabase; decimal result; valueFromDatabase = DBNull.Value; result = (decimal)(valueFromDatabase != DBNull.Value ? valueFromDatabase : 0); result = (valueFromDatabase != DBNull.Value ? (decimal)valueFromDatabase : (decimal)0); The first result evaluation throws an InvalidCastException whereas the second one does not. What is the difference between these two? 回答1: UPDATE: This question was the subject of my blog on May 27th 2010.

How to use LogonUser properly to impersonate domain user from workgroup client

微笑、不失礼 提交于 2019-12-17 10:23:31
问题 ASP.NET: Impersonate against a domain on VMWare This question is what I am asking, but the answer does not provide details on how the _token is derived. It seems to only use WindowsIdentity.GetCurrent().Token so there's no impersonation happening. Can I impersonate a user on a different Active Directory domain in .NET? This next question has conflicting answers, with the accepted one bearing a comment "I'm beginning to suspect that my problem lies elsewhere." Not helpful. LogonUser works only