Wednesday, February 4, 2009

The FileUpload Control

Well it has been awhile, lots going on. Today I was thumbing around some of the apps that I have created in the last year or so and found this little gem. I have had the need to upload as many as 6 pictures into database and folder locations at the same time before, and the saving grace was the FileUpload Control. It has built in functionality that will allow you to manipulate the file to do pretty much anything you want. I will upload one of my better examples later this evening.


For your code behind you want to do something like:

private void btnSubmit_Click(object sender, System.EventArgs e)
{
if (imgUpload.HasFile)
try
{
string serverFileName = Path.GetFileName(imgUpload.PostedFile.FileName);
//imgUpload.PostedFile.SaveAs(@"c:\" + serverFileName);
imgUpload.PostedFile.SaveAs(MapPath(".") + serverFileName);
}
catch (Exception err)
{
thorw err
}
}
}

Wednesday, July 16, 2008

Open File - Security Warning

Ever get that annoying error when trying to run an app from a server or system with a server os installed? Well, if this is a site that you absolutely trust, you know you will not get any virus's or other unwanted attention, then here is a wonderful way to do it in code.

The precursor to this was that I am still developing my main install application. Since it runs .exe files from my server, I was always getting prompted to run the exe. This makes a unattended installation almost impossible. I tried setting the security zones, put no luck. Finally I found out that if you add the sever to you internet options trusted sites list, you can by-pass this prompt.

These settings are, of course, saved in the Registry, and therefore you can programatically change the setting. And if you are worried about the setting, you can always set it back to nothing before the app closes.

Make sure that you have using Microsoft.Win32; declared
Here goes the code:


//disable the Open File - Security Warning in Windows
private void _disableSecurity()
{

try
{
//Declare Variables
RegistryKey _rKey;
object _kPath;

//Open registryKey and get the value of the Domains Key

_rKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\InternetSettings\ZoneMap");

_kPath = _rKey.GetValue("Domains");

//if _kPath is empty then add your Registry key.
if (_kPath == null)
{

//Create the Top Level Domain Key
_rKey = Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet
Settings\ZoneMap\Domains\Your Top Level
Domain
"
);

//Create the SubDomain key
_rKey =
Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet
Settings\ZoneMap\Domains\TLD\Your SubDomain"
);

//beacause we are adding a server, the String name is file, and the object value is 2
_rKey.SetValue("file", 2);
}
}

//catch any exceptions and display them in a popup message box
catch (Exception ex)
{

//writes the message and stacktrace from the error to a messagebox
MessageBox.Show(ex.Message + ex.StackTrace, "Error writing to Registry");
}
}



Thanks and enjoy.... Chuck


Monday, July 14, 2008

Environmental Variables

I am kinda new to C# development, as I have only been doing it for about 3-4 months at this time. I have been programming for about 3 years, 2 of which where while I was in school. My main focus is on Web Development, but recently I have been put in a position that deals with a great amount of windows development.

Anyways, that all aside, I was asked to create a program that would install a huge amount of applications, I think I am up to 45 now... and there are still more. Some of the apps that I am installing are BizTalk 2004, CRM 3.0, all of the VS apps, and so on.

I ran into a problem while installing several of my apps, they were not getting their values updated in the Path Environmental Variable. So I needed a way to do this in C#, and thought, why not just use the Win32 class. So here is what I have come up with:


try
{
RegistryKey _rKey;
object _kPath;

_rKey =
Registry.LocalMachine.OpenSubKey(@"path to Variable");
_kPath = _rKey.GetValue("Path");

_rKey.SetValue("Path", @"String Variable Here");
}
catch (Exception ex)
{
"Handle your Exceptions here"
}

I always wrap my code in Try\Catch statements, but that is just me. Also, make sure that you add the using statment to your app:

using Microsoft.Win32;

After that you declare your variables, you need both a RegistryKey variable and an object variable. We will get the Environmental Variable using the Regkey, and will get\set the value of the key with the object:

RegistryKey _rKey;
object _kPath;

set key equal to the subkey that contains your variable:

_rKey = Registry.LocalMachine.OpenSubKey(@"path to Variable");

to modify the Path Value you would use:
Registry.LocalMachine.OpenSubKey(@"System\CurrentControlSet\Control\Session Manager\Environment");

Next set your object equal to the regkey variable value that you need:

_kPath = _rKey.GetValue("Path");

Notice that this is where we actually point to the Path Value of the System Environmental variable. GetValue, does just that, gets the current value of the key. You have to get the value before you can set it. Otherwise you will not know what to set.

After that you simply set the value data that you want to use:

_rKey.SetValue("Path", @"String Variable Here");

This is where you need to copy the current values of the variable, and then append your new path variables to the end.


That's it. It is pretty easy to use, and gets the job done. I am sure there are other ways to do this, but for 5 lines of code... this is the quickest way I have seen yet.

Thanks,
Chuck