Ruso_aka_Sova

Hi, I am really newbie in the world of C#, so may be it's just some line that should be added but I have no idea :(

ISo am making a litle C# application that should take the clipboard image and save it into the network drive (shared folder).

If the program is executed from the local location (local drive) everything works fine. But when I try to execute the same file from the network (maped drive) it throw me an error:

Unhandled Exception: System.NullReferenceException: Object reference not set toan instance of an object.
at MyClass.Main()


Here is a code:

[code]
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;


class MyClass
{
static void Main ()
{


if (Clipboard.GetDataObject() != null)
{
IDataObject data = Clipboard.GetDataObject();

if (data.GetDataPresent(DataFormats.Bitmap))
{
Image image = (Image)data.GetData(DataFormats.Bitmap,true);
image.Save(@"\\servername\Pasted_Images\printscreen.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);

//System.Diagnostics.Process.Start(@"f:\paste.vbs");

}else{
MessageBox.Show("Please make sure the data you want to paste is an image","Ctest");
}
}else{
MessageBox.Show("No data was found", "Ctest");
}
}
[/code]

Any help is gladly appreciated.
}

Any help is gladly appreciated.


Re: Visual C# General C# application executes well while on local drive, but fails when executed from the network. :(

Peter Ritchie

Normally network drives don't have full trust permission so some methods/classes don't work.

Other than that, it would be helpful to know *where* in your code the error occurs; without that, we have no idea what the cause is.




Re: Visual C# General C# application executes well while on local drive, but fails when executed from the network. :(

Kingsley Faulkner

Hi Ruso,

You are falling victim to .NET's Code Access Security (CAS). Basically because your network drive is in the 'Local Intranet' zone by default your assembly will be executed in a partial trust environment.

The best solution for this is to strongly sign you assembly with a key and then using the .NET Framework 2.0 Configuration tool in Administrative Tools on the computer that would like to execute go into 'Configure Code Access Security Policy', navigate into Code Groups -> All_Code -> LocalIntranet_Zone and add a Child Code Group. In the child code group use the 'Strong Name' condition type and use the public key from your assembly and give it the FullTrust permission set.

Using this technique will grant FullTrust to any assembly you sign with your key that is executed on your LAN. The FullTrust permission set will allow the assembly to do everything it normally can do on your computer.

Hope that helps!

Kingsley





Re: Visual C# General C# application executes well while on local drive, but fails when executed from the network. :(

Ruso_aka_Sova

Thanks, Kingsley Faulkner. That worked fine.
But the the problemm is that we have about 70 workstations on our intranet zone. And the question I have is it possible to change the security settings remotly on each machine I was trying to find some information about registry values for .NET configuration to write a lil vbs script but no luck. Do you have any idea if its possible

Thnaks in advance.




Re: Visual C# General C# application executes well while on local drive, but fails when executed from the network. :(

Ruso_aka_Sova

Thanks, Kingsley Faulkner. That worked fine.
But the the problemm is that we have about 70 workstations on our intranet zone. And the question I have is it possible to change the security settings remotly on each machine I was trying to find some information about registry values for .NET configuration to write a lil vbs script but no luck. Do you have any idea if its possible

Thnaks in advance.




Re: Visual C# General C# application executes well while on local drive, but fails when executed from the network. :(

Kingsley Faulkner

Ah; well if you have a method to push deployment scripts to these machines then I would suggest that you script a call to c:\windows\Microsoft.NET\Framework\vX.X.XXXX\caspol.exe which is the command line interface to CAS.

Kingsley