blah blah blah is here! blah blah » Close

up0down
link

I'm using VS 2008 Express edition.....

string filename;
private void button3_Click(object sender, EventArgs e)
{
Form3 f3 = (Form3)Application.OpenForms["Form3"];
ComboBox cb1 = (ComboBox)f3.Controls["comboBox1"];
if (cb1.SelectedItem.ToString() == "General Instrument")
{
filename = "W:\\sourabh\\catalog.pdf";
}
try
{
catalog = Process.Start(filename);
}
catch (Exception ex)
{
MessageBox.Show("\n" + ex, "Error");
}
}


In the above code the variable filename is not accessible in the try-catch block. Why?

------------------------------------------------

If I edit the above code by adding the complete path in Process.Start(). Then if this application is accessed on any other computer on network, then it gives an error in the Process.Start() function: Request Failed.
See the end of this message for details on invoking 
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.Security.SecurityException: Request failed.
at System.Security.CodeAccessSecurityEngine.ThrowSecurityException(Assembly asm, PermissionSet granted, PermissionSet refused, RuntimeMethodHandle rmh, SecurityAction action, Object demand, IPermission permThatFailed)
at System.Security.CodeAccessSecurityEngine.ThrowSecurityException(Object assemblyOrString, PermissionSet granted, PermissionSet refused, RuntimeMethodHandle rmh, SecurityAction action, Object demand, IPermission permThatFailed)
at System.Security.CodeAccessSecurityEngine.CheckSetHelper(PermissionSet grants, PermissionSet refused, PermissionSet demands, RuntimeMethodHandle rmh, Object assemblyOrString, SecurityAction action, Boolean throwException)
at System.Security.CodeAccessSecurityEngine.CheckSetHelper(CompressedStack cs, PermissionSet grants, PermissionSet refused, PermissionSet demands, RuntimeMethodHandle rmh, Assembly asm, SecurityAction action)
at Instruments.Form4.button3_Click(Object sender, EventArgs e)
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
The action that failed was:
LinkDemand
The type of the first permission that failed was:
System.Security.PermissionSet
The Zone of the assembly that failed was:
Intranet


************** Loaded Assemblies **************
mscorlib
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.832 (QFE.050727-8300)
CodeBase: file:///C:/WINDOWS/Microsoft.NET/Framework/v2.0.50727/mscorlib.dll
----------------------------------------
Instruments
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///W:/sourabh%20malviya/Instruments.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.832 (QFE.050727-8300)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Windows.Forms/2.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.832 (QFE.050727-8300)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System/2.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Drawing
Assembly Version: 2.0.0.0
Win32 Version: 2.0.50727.832 (QFE.050727-8300)
CodeBase: file:///C:/WINDOWS/assembly/GAC_MSIL/System.Drawing/2.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

last answered one year ago

1 answers

up0down
link

First question: You probably need to instantiate the variable filename before using it. The compiler knows that there's a chance it will be null once the code gets inside the try/catch block. Do this:

string filename = "";
private void button3_Click(object sender, EventArgs e)
{
Form3 f3 = (Form3)Application.OpenForms["Form3"];
ComboBox cb1 = (ComboBox)f3.Controls["comboBox1"];
if (cb1.SelectedItem.ToString() == "General Instrument")
{
filename = "W:\\sourabh\\catalog.pdf";
}
try
{
catalog = Process.Start(filename);
}
catch (Exception ex)
{
MessageBox.Show("\n" + ex, "Error");
}
}


The exception you're getting sounds like something to do with access rights on the folder you're accessing. Try another folder or review the security settings on that folder.

Feedback