.net

How to have a program know where it has been launched?

Imagine I have a picture viewer application made with C# and .NET. I have already set the preferred application to view pictures to use the C# application. I want to somehow let my program know where it has been invoked. How can I achieve this? ...

Is there a way to execute initializer code before each test in Visual Studio Unit Testing (MSTest) for all tests in a project?

Hi everyone, I have a Unit Test project with 20+ .cs files. I want to run some setup code before each individual test. Kinda like how the [TestInitialize] attribute works. However, I'd need to put that attribute on all 20+ of my .cs files. Is there a way to centralize the initializing code in one place for every test in my entire pr...

How to get a stack trace in .NET in normal execution?

In VB .NET, I know I can get a stack trace by looking at the value of ex.StackTrace when handling an exception. How can I get the functions on the stack when I am not handling an exception? I am looking to implement a logging system of some sort to record the steps the user takes prior to a crash to assist in debugging. ...

A function to return a sign

I know this will really turn out to be simple, but my brain is just not working. I need a function in C# that will return -1 if the integer passed to the function has a negative sign, return 1 if the integer has a positive sign and return 0 if the number passed is 0. So for example: int Sign=SignFunction(-82); // Should return -1 int Si...

How do I programmatically check the date of a file in a Zip archive?

Given that I have a zip file called archive.zip that contains a file called customerData, how can I programmatically check the date of the file inside archive.zip? I'm using the command-line Winzip utility wzunzip, but I wouldn't object to possibly using something else. I'm writing a .net application that will periodically read data fr...

SQL CLR how to access username, hostname, and app_name running in safe mode

I have a trigger written in sql clr (sql 2005, .net 3.5) and I need to get at username, hostname and app_name. In TSQL I would simply use select suser_name() select host_name() select APP_NAME() In my .Net code I tried System.Security.Principal.WindowsIdentity.GetCurrent().Name System.Environment.MachineName System.AppDomain.Current...

How to get files in a relative path in C#

If I have an executable called app.exe which is what I am coding in C#, how would I get files from a folder loaded in the same directory as the app.exe, using relative paths? This throws an illegal characters in path exception. string [ ] files = Directory.GetFiles ( "\Archive\*.zip" ); How would one do this in C#? ...

Is there a threadsafe and generic IList<T> in c#?

Is List<T> or HashSet<T> or anything else built in threadsafe for addition only? My question is similar to http://stackoverflow.com/questions/1278010/threadsafe-and-generic-arraylist-in-c but I'm only looking for safety to cover adding to this list threaded, not removal or reading from it. ...

WPF/Silverlight How To Calculate total cubic units/pixels in a control?

I have a scribbale inkpresenter in my silverlight app that I'm using multiple times in different shapes, I want to calculate how much percent of the inkpresenter has been scribbled, My problem is that since the shape of the ink presenter can be circular or poly I don't know how i can get the units/pixels there is available on the scrib...

Reusing Expression in Linq select clause (query format)

I have an Expression that converts one type of object to another type. The expression is as follows: public Expression<Func<SQLRepository.ActionType, Model.ActionType>> DBActionTypeToActionType = (SQLRepository.ActionType at) => new Model.ActionType() { ID = at.OID, DisplayName = at.DisplayName }; I can use the Expression like...

System.Net.Mail send message without recipient

I am using System.Net.Mail.MailMessage to create and send an email message. For various reasons I do not want to use the .To.Add() or the .CC.Add() or the .Bcc.Add() methods to add recipients to the email. I want to add them via the .Headers.Add() method. (This gives greater control over the recipients). However, when I come to send t...

Why are the unsigned CLR types so difficult to use in C#?

I came from a mostly C/C++ background before I began using C#. One of the things I did with my first project in C# was make a class like this class Element{ public uint Size; public ulong BigThing; } I was then mortified by the fact that this requires casting: int x=MyElement.Size; as does int x=5; uint total=MyElement.Size+x;...

Using LINQ to find all keys from one collection that are not in another?

Hey all, I'm trying to locate all the keys in one Dictionary that are not in another Dictionary. Obviously, I can do this using a nested loop, but I'm trying to learn LINQ at the moment and I was wondering if I might use it to accomplish this task? Here's what I have so far: Dictionary<string, List<string>> DBtables = this.CollectTab...

Problem with MSHTML COM clicking on submit button

I'm having a problem screenscraping some data from this website using the MSHTML COM component. I have a WebBrowser control on my WPF form. The code where I retrieve the HMTL elements is in the WebBrowser LoadCompleted events. After I set the values of the data to the HTMLInputElement and call the click method on the HTMLInputButtonEleme...

One Liner: WeakReference-to-a-Lambda Event Handler

Hello, Can you see downsides to this one-liner other than the fact that multiple uses of it would violate the DRY principle? It seems straightforward but the fact that I haven't seen others propose it makes me wonder if there's a downside to it. This bit of code creates a WeakReference to a method and then registers an event handler th...

Web User Control validation doesn't stop page events

This is a weird problem. I have created Web User Control with two TextBoxes and two CustomValidators (server-side only). I have also implemented IValidator interface and created ValidationGroup property that deafults to null. The problem is that when I put this control on a website it doesn't block events when it's not valid. I've set C...

Shared Method in Parent Pulling Info From and Producing Child?

Here's what I'd like to achieve. After mucking around with it for a day and a half, though, I've become nearly convinced that it can't be done. If someone can show me how to achieve this, I'd be quite grateful. I have a .NET parent class Parent with a shared method Fetch(). Fetch can take a variety of parameters to help it know what par...

WPF Webcam component crashes under .NET 4.0

I've got a WPF webcam component's source-code from Codeplex that I'm trying to port from .NET 3.5 to .NET 4.0. Compiles just fine under both targets, but in the .NET 4.0 version, it crashes when running the app with a System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception at the line moniker...

Are there good third party EDMX generator/updater tools out there?

Are there good third party EDMX generator/updater tools out there? The visual studio EDMX is driving nuts. I constantly have to restart VS2008 in order to get the EDMX to properly update. Are there any tools out there people like? This is somewhat subjective but I figured it would be ok to ask nonetheless ...

Default resource for data annotations in ASP.NET MVC

There's a way to set the default resource to the data annotations validations? I don't wanna make something like this: [Required(ErrorMessage="Name required.", ErrorMessageResourceType=typeof(CustomDataAnnotationsResources)] public string Name { get; set; } I would like something like this: Global.asax DataAnnotations.DefaultResour...