Illustrates refusing permissions

image_pdfimage_print


   

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/


/*
  Example19_4.cs illustrates refusing permissions
*/

using System;
using System.IO;
using System.Security.Permissions;

[assembly:FileIOPermissionAttribute(SecurityAction.RequestRefuse,
Unrestricted=true)]

public class Example19_4 
{

    public static void Main() 
    {

        // Create a new file to work with
        FileStream fsOut = File.Create(@"c:	emp	est.txt");
        // Create a StreamWriter to handle writing
        StreamWriter sw = new StreamWriter(fsOut);
        // And write some data
        sw.WriteLine("'Twas brillig, and the slithy toves");
        sw.WriteLine("Did gyre and gimble in the wabe.");
        sw.Flush();
        sw.Close();
    }
}
           
          


Illustrates requesting a permission set

image_pdfimage_print
   

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/
/*
  Example19_3.cs illustrates requesting a permission set
*/

using System;
using System.IO;
using System.Security.Permissions;

[assembly:PermissionSetAttribute(SecurityAction.RequestMinimum,
 Name="FullTrust")]

public class Example19_3 
{

    public static void Main() 
    {

        // Create a new file to work with
        FileStream fsOut = File.Create(@"c:	emp	est.txt");
        // Create a StreamWriter to handle writing
        StreamWriter sw = new StreamWriter(fsOut);
        // And write some data
        sw.WriteLine("'Twas brillig, and the slithy toves");
        sw.WriteLine("Did gyre and gimble in the wabe.");
        sw.Flush();
        sw.Close();

    }

}
           
          


Illustrates requesting optional permissions

image_pdfimage_print
   

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
  Example19_2.cs illustrates requesting optional permissions
*/

using System;
using System.IO;
using System.Security.Permissions;

[assembly:FileIOPermissionAttribute(SecurityAction.RequestOptional,
All=@"c:	emp")]

public class Example19_2 
{

    public static void Main() 
    {

        // Create a new file to work with
        FileStream fsOut = File.Create(@"c:	emp	est.txt");
        // Create a StreamWriter to handle writing
        StreamWriter sw = new StreamWriter(fsOut);
        // And write some data
        sw.WriteLine("'Twas brillig, and the slithy toves");
        sw.WriteLine("Did gyre and gimble in the wabe.");
        sw.Flush();
        sw.Close();
    }
}
          
          


Illustrates requesting minimum permissions

image_pdfimage_print
   

/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy

Publisher: Sybex;
ISBN: 0782129110
*/

/*
  Example19_1.cs illustrates requesting minimum permissions
*/

using System;
using System.IO;
using System.Security.Permissions;

[assembly:FileIOPermissionAttribute(SecurityAction.RequestMinimum,
 All=@"c:	emp")]

public class Example19_1 
{

    public static void Main() 
    {

        // Create a new file to work with
        FileStream fsOut = File.Create(@"c:	emp	est.txt");
        // Create a StreamWriter to handle writing
        StreamWriter sw = new StreamWriter(fsOut);
        // And write some data
        sw.WriteLine("'Twas brillig, and the slithy toves");
        sw.WriteLine("Did gyre and gimble in the wabe.");
        sw.Flush();
        sw.Close();

    }

}




           
          


Platform Helper

image_pdfimage_print
   
 
// Taken from....
//
// NAnt - A .NET build tool
// Copyright (C) 2001-2003 Gerry Shaw
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

// Jaroslaw Kowalski (jkowalski@users.sourceforge.net)

using System;
using System.Runtime.InteropServices;
using System.Text;

namespace Dotnet.Commons.IO {
    [Serializable()]
    internal class PlatformHelper {
        public static readonly bool IsMono;
        public static readonly bool IsWin32;
        public static readonly bool IsUnix;
        public static readonly bool PInvokeOK;
        
        static PlatformHelper() {
            // check a class in mscorlib to determine if we're running on Mono
            if (Type.GetType("System.MonoType", false) != null) {
                // we're on Mono
                IsMono = true;
            }
            
            PlatformID platformID = Environment.OSVersion.Platform;

            if (platformID == PlatformID.Win32NT || platformID == PlatformID.Win32Windows) {
                IsWin32 = true;
            }

            // check for (non-)Unix platforms - see FAQ for more details
            // http://www.mono-project.com/FAQ:_Technical#How_to_detect_the_execution_platform_.3F
            int platform = (int) Environment.OSVersion.Platform;
            if (platform == 4 || platform == 128) {
                IsUnix = true;
            }

            if (IsWin32 && !IsMono) {
                PInvokeOK = true;
            }
        }
      
        private class PInvokeHelper {
            [DllImport("kernel32.dll")]
            private static extern long GetVolumeInformation(string PathName, StringBuilder VolumeNameBuffer, UInt32 VolumeNameSize, ref UInt32 VolumeSerialNumber, ref UInt32 MaximumComponentLength, ref UInt32 FileSystemFlags, StringBuilder FileSystemNameBuffer, UInt32 FileSystemNameSize);

            public static long GetVolumeInformationWrapper(string PathName, StringBuilder VolumeNameBuffer, UInt32 VolumeNameSize, ref UInt32 VolumeSerialNumber, ref UInt32 MaximumComponentLength, ref UInt32 FileSystemFlags, StringBuilder FileSystemNameBuffer, UInt32 FileSystemNameSize) {
                return GetVolumeInformation(PathName, VolumeNameBuffer, VolumeNameSize, ref VolumeSerialNumber, ref MaximumComponentLength, ref FileSystemFlags, FileSystemNameBuffer, FileSystemNameSize);
            }
        }
    }
}

   
     


StartupPath

image_pdfimage_print
   
  

using System;
using System.Windows.Forms;

class MainClass {
    static void Main(string[] args) {

        OpenFileDialog dlg = new OpenFileDialog();
        dlg.InitialDirectory = Application.StartupPath;

        if (dlg.ShowDialog() == DialogResult.OK) {
            Console.WriteLine(dlg.FileName);

        }
    }
}

   
     


Removing an Installed Message Filter

image_pdfimage_print
   
 


using System;
using System.Windows.Forms;
   
public class BlockLeftMouseButtonMessageFilter : IMessageFilter
{
    const int WM_LBUTTONDOWN = 0x201;
    
    public bool PreFilterMessage(ref Message m){
        if(m.Msg == WM_LBUTTONDOWN)
        {
            Console.WriteLine("The left mouse button is down.");
            Application.RemoveMessageFilter(this);
            return true;
        }
        return false;
    }
}
   
public class MainForm : Form
{
    public static void Main()
    {
        MainForm MyForm = new MainForm();
        BlockLeftMouseButtonMessageFilter MsgFilter = new BlockLeftMouseButtonMessageFilter();
        Application.AddMessageFilter(MsgFilter);
        Application.Run(MyForm);
    }
   
    public MainForm()
    {
        Text = "Message Filter Removal Test";
    }
}