I had some files that I wanted to embed into my project and access in code. I needed both images and xml files.
First you have to add the files to the project. I create the images in paint before importing them. The xml files were created within Visual Studio. Things started to look a little cluttered, so I created folders in the project to group them up.
Once the files are in place you need to open the properties for each one. Change the build action to "Embedded Resource". Now we can access the file streams from code.
image warningImage = new Bitmap(this.GetType().Assembly.GetManifestResourceStream("Application1.images.warning.bmp"));
StreamReader xmlFile = new StreamReader(this.GetType().Assembly.GetManifestResourceStream("Application1.foldername.template.xml"));
GetManifestResourceStream returns a stream. There is a overload for the bitmap constructor that takes a stream if its an image you are working with.
Some problems you just can't search on. Here are some I wish were more searchable and this blog is my attempt to make that happen.
Tuesday, December 18, 2007
Friday, December 14, 2007
NullReferenceException raising an event in C#
I was working with events in C# and ran into this exception. System.NullReferenceException was unhandled Message="Object reference not set to an instance of an object." This exception was at the exact point in my code where I attempted to raise the event. It turns out that if there are not event handlers attached to your event, an attempt to raise it will give you this exception. To solve it, you have to check to see if its null before raising it.
All the samples I was looking at did not check for a null value, so neither did I. Now that I had the problem, I see why I need to check.
public class TestEvent{
public event EventHandler MyEvent;
public void RaiseMyEvent(){
if(MyEvent != null){
MyEvent(this, null);
}
}
}
All the samples I was looking at did not check for a null value, so neither did I. Now that I had the problem, I see why I need to check.
Sunday, December 09, 2007
Running vbs from .net code
There are a few ways to run vbscript inside vb.net. If you just need the script to run, you can use the process object.
Process Class
If you have some VBScript that you want to intereact with variables or run on the fly. Microsoft has a ScriptControl that allows you to do just that. The script control has 3 options of running code.
Dim oProcess as new Process
Try
oProcess.StartInfo.UseShellExecute = True
oProcess.StartInfo.FileName = "CScript c:\script.vbs //nologo"
oProcess.Start()
oProcess.WaitForExit()
Finally
oProcess.Dispose()
End Try
Process Class
If you have some VBScript that you want to intereact with variables or run on the fly. Microsoft has a ScriptControl that allows you to do just that. The script control has 3 options of running code.
- Eval: Evaluates a text expression.
- Run: Runs a named Sub or Function.
- Execute: Executes a script statement.
You can find more information on the ScriptControl at Microsoft.
Q184740: How To Call Functions Using the Script Control
Q184739: INFO: Where to Obtain the Script Control
If you are looking to run vb.net code inside vbscript, see my other post:
Running VB.NET code in VBScript or other apps
Friday, December 07, 2007
Raising an event in C#
Here is the simplest way to raise an event.
This uses the default EventHandler that returns a object source and a EventArgs ex. If you want to pass back different parameters, you will need to create your own delegate. The delegate we just used looks like this:
We dont have to define it because .NET already defined it for us. Here is a second example where we built a custom delegate and raise it.
Now that I explained how to raise events, there is a bug in that code. I'm saving that for my next post: NullReferenceException raising an event in C#
public class TestEvent{
public event EventHandler MyEvent;
public void RaiseMyEvent(){
MyEvent(this, null);
}
}
This uses the default EventHandler that returns a object source and a EventArgs ex. If you want to pass back different parameters, you will need to create your own delegate. The delegate we just used looks like this:
public delegate void EventHandler (object source, EventArgs ex);
We dont have to define it because .NET already defined it for us. Here is a second example where we built a custom delegate and raise it.
public delegate void MyCustomEventHandler (string message);
public class TestEvent2{
public event MyCustomEventHandler MyEvent2;
public void RaiseMyEvent(){
MyEvent2("Message sent with event");
}
}
Now that I explained how to raise events, there is a bug in that code. I'm saving that for my next post: NullReferenceException raising an event in C#
Saturday, November 17, 2007
Inconsistent accessibility: base class is less accessible than class
I was doing some coding in c#. I had one class but decided to split it up so I could reuse some of the functionality later. So I made a simple shell and started moving pieces into the parent class.
On my first compile I got this message: Inconsistent accessibility: base class is less accessible than class
I am a little rusty with my C# and classes, but it turns out I left off the public keyword on the new class. Adding public fixed that compile error.
On my first compile I got this message: Inconsistent accessibility: base class is less accessible than class
I am a little rusty with my C# and classes, but it turns out I left off the public keyword on the new class. Adding public fixed that compile error.
Tuesday, November 13, 2007
Mapped drives missing in explorer.
We ran into a problem with our mapped drives on our network. First time logins failed to display in explorer until they logged in the second time. You can see them from a command or dos prompt. The disconnect drives dialog also showed the mappings. I can also kill the explorer.exe process and when I run it from the task manager, the mapping show up.
Running the logon scripts manually will make the drives show up. It looks like a timing issue. If my script maps a lot of drives, the earlier ones are the ones that don’t show up. Any that map after explorer loads work fine. It did not matter if it was the users individual logon script or the group policy log on script. Different logon scripts didn’t fix it either. I have a custom vb script that I use, but I also tested it with a batch file full of NET USE commands.
I have worked on this issue for a long time. Searching different ways and posting to various forums. Finally Lanwench [MVP - Exchange] responded to my problem on microsoft’s technet newsgroup on Windows XP Networking. His solution was to enable the “Always Wait for Network” GPO setting and linked me to the detailed reason for that.
Always wait for the network at computer startup and logon
Administrative Template: System
Policy Node: MACHINE
Policy Path: Administrative Templates\System\Logon
Supported On: At least Microsoft Windows XP Professional or Windows Server 2003 family
Help/Explain Text: Determines whether Windows XP waits for the network during computer startup and user logon. By default, Windows XP does not wait for the network to be fully initialized at startup and logon. Existing users are logged on using cached credentials, which results in shorter logon times. Group Policy is applied in the background once the network becomes available. Note that because this is a background refresh, extensions such as Software Installation and Folder Redirection take two logons to apply changes. To be able to operate safely, these extensions require that no users be logged on. Therefore, they must be processed in the foreground before users are actively using the computer. In addition, changes that are made to the user object, such as adding a roaming profile path, home directory, or user object logon script, may take up to two logons to be detected. If a user with a roaming profile, home directory, or user object logon script logs on to a computer, Windows XP always waits for the network to be initialized before logging the user on. If a user has never logged on to this computer before, Windows XP always waits for the network to be initialized. If you enable this setting, logons are performed in the same way as for Windows 2000 clients, in that Windows XP waits for the network to be fully initialized before users are logged on. Group Policy is applied in the foreground, synchronously. If you disable or do not configure this setting, Windows does not wait for the network to be fully initialized and users are logged on with cached credentials. Group Policy is applied asynchronously in the background. Note: If you want to guarantee the application of Folder Redirection, Software Installation, or roaming user profile settings in just one logon, enable this setting to ensure that Windows waits for the network to be available before applying policy. Note: For servers, the startup and logon processing always behaves as if this policy setting is enabled.
Registry Settings: HKLM\Software\Policies\Microsoft\Windows NT\CurrentVersion\Winlogon!SyncForegroundPolicy
Running the logon scripts manually will make the drives show up. It looks like a timing issue. If my script maps a lot of drives, the earlier ones are the ones that don’t show up. Any that map after explorer loads work fine. It did not matter if it was the users individual logon script or the group policy log on script. Different logon scripts didn’t fix it either. I have a custom vb script that I use, but I also tested it with a batch file full of NET USE commands.
I have worked on this issue for a long time. Searching different ways and posting to various forums. Finally Lanwench [MVP - Exchange] responded to my problem on microsoft’s technet newsgroup on Windows XP Networking. His solution was to enable the “Always Wait for Network” GPO setting and linked me to the detailed reason for that.
Always wait for the network at computer startup and logon
Administrative Template: System
Policy Node: MACHINE
Policy Path: Administrative Templates\System\Logon
Supported On: At least Microsoft Windows XP Professional or Windows Server 2003 family
Help/Explain Text: Determines whether Windows XP waits for the network during computer startup and user logon. By default, Windows XP does not wait for the network to be fully initialized at startup and logon. Existing users are logged on using cached credentials, which results in shorter logon times. Group Policy is applied in the background once the network becomes available. Note that because this is a background refresh, extensions such as Software Installation and Folder Redirection take two logons to apply changes. To be able to operate safely, these extensions require that no users be logged on. Therefore, they must be processed in the foreground before users are actively using the computer. In addition, changes that are made to the user object, such as adding a roaming profile path, home directory, or user object logon script, may take up to two logons to be detected. If a user with a roaming profile, home directory, or user object logon script logs on to a computer, Windows XP always waits for the network to be initialized before logging the user on. If a user has never logged on to this computer before, Windows XP always waits for the network to be initialized. If you enable this setting, logons are performed in the same way as for Windows 2000 clients, in that Windows XP waits for the network to be fully initialized before users are logged on. Group Policy is applied in the foreground, synchronously. If you disable or do not configure this setting, Windows does not wait for the network to be fully initialized and users are logged on with cached credentials. Group Policy is applied asynchronously in the background. Note: If you want to guarantee the application of Folder Redirection, Software Installation, or roaming user profile settings in just one logon, enable this setting to ensure that Windows waits for the network to be available before applying policy. Note: For servers, the startup and logon processing always behaves as if this policy setting is enabled.
Registry Settings: HKLM\Software\Policies\Microsoft\Windows NT\CurrentVersion\Winlogon!SyncForegroundPolicy
Friday, November 02, 2007
Christmas Wish List
People keep asking for a wish list for Christmas. I'm a hard person to buy for sometimes. I usually don't need very much. When I do see something I want, I have a hard time not buying it early.
I recently purchased a Nintendo Wii. That does open a lot of options for gifts. I made a wish list on amazon that contains the Wii stuff I want.
My Wish List
Each game adds different value to the Wii.
Super Mario Galaxy
: Every Nintendo has a Mario platform game that pules you into the system. Its a showcase of design and interaction with the system. This game it exactly that. Mario is new every time its released and becomes a must have game.
Mario Party 8
: With the focus of the Wii around group interaction and fun, this game builds on that even more. With a lot of mini games and the option to play 4 people at once, your group will have lots of fun on this one.
Metroid Prime 3 Corruption
: Another household Nintendo name that never lets you down. This is a highly interactive first person single player adventure.
Super Smash Bros. Brawl
: to be released just after Christmas, its a arcade fighting style game. It pulls in all the classic characters from several popular titles and puts them in the ring against each other.
Super Monkey Ball Banana Blitz
: A Sega release that places you in control of a glass ball. You tilt and turn the ground under the ball to control its movements. sounds like its easy to pick up and learn. The difficulty gets insane by the time you finish.
I recently purchased a Nintendo Wii. That does open a lot of options for gifts. I made a wish list on amazon that contains the Wii stuff I want.
My Wish List
Each game adds different value to the Wii.
Super Mario Galaxy
Mario Party 8
Metroid Prime 3 Corruption
Super Smash Bros. Brawl
Super Monkey Ball Banana Blitz
Subscribe to:
Posts (Atom)