Hello,
Thanks a lot. When I tried opening file chosen in the open file dialog, it
did give security exception.
I've one more similar piece of code with similar problem. It is as follows:
CodeAccessPermission perm = new
FileIOPermission(FileIOPermissionAccess.Write,"C:\\");
try
{
perm.Deny();
StreamWriter sw = new StreamWriter("C:\\myfile.txt",true);
sw.WriteLine("***************************************");
sw.Close();
}
catch (SecurityException ex)
{
MessageBox.Show(ex.Message);
}
This code ALWAYS writes to the specified file without any SecurityException.
What could be the reason?
Thanks in advance
Sameeksha
[quoted text, click to view] "Nicole Calinoiu" wrote:
> Sameeksha,
>
> The OpenFileDialog class only demands the FileDialogPermission when its
> OpenFile method is called, not when the dialog is displayed. Doesn't
> necessarily make much sense, but that's the the way it's implemented...
> That said, you can get your desired result by denying UIPermission, but
> that's not necessarily worthwhile since denials can be overridden by
> assertions on the same call stack.
>
> HTH,
> Nicole
>
>
> "Sameeksha" <Sameeksha@discussions.microsoft.com> wrote in message
> news:DE4D2941-1AE5-464B-BAC9-63CA7631776E@microsoft.com...
> > Hello All,
> >
> > I wrote the following code in a button click event in a windows
> > application.
> >
> > CodeAccessPermission perm = new
> > FileDialogPermission(FileDialogPermissionAccess.Open);
> > perm.Deny();
> > try
> > {
> > OpenFileDialog dlgOpen = new OpenFileDialog();
> > dlgOpen.ShowDialog();
> > }
> > catch (SecurityException ex)
> > {
> > MessageBox.Show("You are not authorized to open file
> > dialog");
> > MessageBox.Show(ex.Message);
> > }
> >
> > Output that I expect is the exception message boxes. But instead it shows
> > the open file dialog box and does not throw any exception. Why should it
> > be
> > able to open file dialog even when permission is explicitly denied?
> > (perm.Deny())
> >
> > Thanks in advance
> > Sameeksha
> > --
> > Sameeksha,
> > MCP (.NET)
>
>
Sameeksha,
The OpenFileDialog class only demands the FileDialogPermission when its
OpenFile method is called, not when the dialog is displayed. Doesn't
necessarily make much sense, but that's the the way it's implemented...
That said, you can get your desired result by denying UIPermission, but
that's not necessarily worthwhile since denials can be overridden by
assertions on the same call stack.
HTH,
Nicole
[quoted text, click to view] "Sameeksha" <Sameeksha@discussions.microsoft.com> wrote in message
news:DE4D2941-1AE5-464B-BAC9-63CA7631776E@microsoft.com...
> Hello All,
>
> I wrote the following code in a button click event in a windows
> application.
>
> CodeAccessPermission perm = new
> FileDialogPermission(FileDialogPermissionAccess.Open);
> perm.Deny();
> try
> {
> OpenFileDialog dlgOpen = new OpenFileDialog();
> dlgOpen.ShowDialog();
> }
> catch (SecurityException ex)
> {
> MessageBox.Show("You are not authorized to open file
> dialog");
> MessageBox.Show(ex.Message);
> }
>
> Output that I expect is the exception message boxes. But instead it shows
> the open file dialog box and does not throw any exception. Why should it
> be
> able to open file dialog even when permission is explicitly denied?
> (perm.Deny())
>
> Thanks in advance
> Sameeksha
> --
> Sameeksha,
> MCP (.NET)
If you want to block appending to an existing file, use
FileIOPermissionAccess.Append, not FileIOPermissionAccess.Write. You can,
of course, use both. e.g.:
CodeAccessPermission perm = new
FileIOPermission(FileIOPermissionAccess.Write |
FileIOPermissionAccess.Append ,"C:\\");
HTH,
Nicole
[quoted text, click to view] "Sameeksha" <Sameeksha@discussions.microsoft.com> wrote in message
news:D0CF529D-D4B7-46E9-B60A-BC667034ACCF@microsoft.com...
> Hello,
>
> Thanks a lot. When I tried opening file chosen in the open file dialog, it
> did give security exception.
>
> I've one more similar piece of code with similar problem. It is as
> follows:
>
> CodeAccessPermission perm = new
> FileIOPermission(FileIOPermissionAccess.Write,"C:\\");
> try
> {
> perm.Deny();
> StreamWriter sw = new StreamWriter("C:\\myfile.txt",true);
> sw.WriteLine("***************************************");
> sw.Close();
> }
> catch (SecurityException ex)
> {
> MessageBox.Show(ex.Message);
> }
> This code ALWAYS writes to the specified file without any
> SecurityException.
> What could be the reason?
>
> Thanks in advance
> Sameeksha
>
> "Nicole Calinoiu" wrote:
>
>> Sameeksha,
>>
>> The OpenFileDialog class only demands the FileDialogPermission when its
>> OpenFile method is called, not when the dialog is displayed. Doesn't
>> necessarily make much sense, but that's the the way it's implemented...
>> That said, you can get your desired result by denying UIPermission, but
>> that's not necessarily worthwhile since denials can be overridden by
>> assertions on the same call stack.
>>
>> HTH,
>> Nicole
>>
>>
>> "Sameeksha" <Sameeksha@discussions.microsoft.com> wrote in message
>> news:DE4D2941-1AE5-464B-BAC9-63CA7631776E@microsoft.com...
>> > Hello All,
>> >
>> > I wrote the following code in a button click event in a windows
>> > application.
>> >
>> > CodeAccessPermission perm = new
>> > FileDialogPermission(FileDialogPermissionAccess.Open);
>> > perm.Deny();
>> > try
>> > {
>> > OpenFileDialog dlgOpen = new OpenFileDialog();
>> > dlgOpen.ShowDialog();
>> > }
>> > catch (SecurityException ex)
>> > {
>> > MessageBox.Show("You are not authorized to open file
>> > dialog");
>> > MessageBox.Show(ex.Message);
>> > }
>> >
>> > Output that I expect is the exception message boxes. But instead it
>> > shows
>> > the open file dialog box and does not throw any exception. Why should
>> > it
>> > be
>> > able to open file dialog even when permission is explicitly denied?
>> > (perm.Deny())
>> >
>> > Thanks in advance
>> > Sameeksha
>> > --
>> > Sameeksha,
>> > MCP (.NET)
>>
>>
>>
Great!!!! It did work. Thanks a lot.
[quoted text, click to view] "Nicole Calinoiu" wrote:
> If you want to block appending to an existing file, use
> FileIOPermissionAccess.Append, not FileIOPermissionAccess.Write. You can,
> of course, use both. e.g.:
>
> CodeAccessPermission perm = new
> FileIOPermission(FileIOPermissionAccess.Write |
> FileIOPermissionAccess.Append ,"C:\\");
>
> HTH,
> Nicole
>
>
> "Sameeksha" <Sameeksha@discussions.microsoft.com> wrote in message
> news:D0CF529D-D4B7-46E9-B60A-BC667034ACCF@microsoft.com...
> > Hello,
> >
> > Thanks a lot. When I tried opening file chosen in the open file dialog, it
> > did give security exception.
> >
> > I've one more similar piece of code with similar problem. It is as
> > follows:
> >
> > CodeAccessPermission perm = new
> > FileIOPermission(FileIOPermissionAccess.Write,"C:\\");
> > try
> > {
> > perm.Deny();
> > StreamWriter sw = new StreamWriter("C:\\myfile.txt",true);
> > sw.WriteLine("***************************************");
> > sw.Close();
> > }
> > catch (SecurityException ex)
> > {
> > MessageBox.Show(ex.Message);
> > }
> > This code ALWAYS writes to the specified file without any
> > SecurityException.
> > What could be the reason?
> >
> > Thanks in advance
> > Sameeksha
> >
> > "Nicole Calinoiu" wrote:
> >
> >> Sameeksha,
> >>
> >> The OpenFileDialog class only demands the FileDialogPermission when its
> >> OpenFile method is called, not when the dialog is displayed. Doesn't
> >> necessarily make much sense, but that's the the way it's implemented...
> >> That said, you can get your desired result by denying UIPermission, but
> >> that's not necessarily worthwhile since denials can be overridden by
> >> assertions on the same call stack.
> >>
> >> HTH,
> >> Nicole
> >>
> >>
> >> "Sameeksha" <Sameeksha@discussions.microsoft.com> wrote in message
> >> news:DE4D2941-1AE5-464B-BAC9-63CA7631776E@microsoft.com...
> >> > Hello All,
> >> >
> >> > I wrote the following code in a button click event in a windows
> >> > application.
> >> >
> >> > CodeAccessPermission perm = new
> >> > FileDialogPermission(FileDialogPermissionAccess.Open);
> >> > perm.Deny();
> >> > try
> >> > {
> >> > OpenFileDialog dlgOpen = new OpenFileDialog();
> >> > dlgOpen.ShowDialog();
> >> > }
> >> > catch (SecurityException ex)
> >> > {
> >> > MessageBox.Show("You are not authorized to open file
> >> > dialog");
> >> > MessageBox.Show(ex.Message);
> >> > }
> >> >
> >> > Output that I expect is the exception message boxes. But instead it
> >> > shows
> >> > the open file dialog box and does not throw any exception. Why should
> >> > it
> >> > be
> >> > able to open file dialog even when permission is explicitly denied?
> >> > (perm.Deny())
> >> >
> >> > Thanks in advance
> >> > Sameeksha
> >> > --
> >> > Sameeksha,
> >> > MCP (.NET)
> >>
> >>
> >>
>
>
Don't see what you're looking for? Try a search.