CAS Security.cpp : FileIOPermission « Security « Visual C++ .NET

Home
Visual C++ .NET
1.2D
2.Class
3.Collections
4.Data Type
5.Database ADO.net
6.Delegate
7.Development
8.File Directory
9.Function
10.Generics
11.GUI Form
12.Language Basics
13.Network
14.Reflection
15.Security
16.Statement
17.Structure
18.Thread
19.XML
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Visual C++ .NET » Security » FileIOPermission 
CAS Security.cpp
 
#include "stdafx.h"
using namespace CASSecurity;

[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
  Application::Run(gcnew Form1());
  return 0;
}


    using namespace System;
    using namespace System::ComponentModel;
    using namespace System::Collections;
    using namespace System::IO;
    using namespace System::Windows::Forms;
    using namespace System::Data;
    using namespace System::Drawing;
  using namespace System::Security::Permissions;


    public ref class Form1 : public System::Windows::Forms::Form
    {
    public:
        Form1(void)
        {
            InitializeComponent();

      try
      {
        (gcnew FileIOPermission(FileIOPermissionAccess::Read, "C:\\Test"))->Demand();
      }
      catch(Exception^)
      {
        bnReadFile->Enabled = false;
      }

      try
      {
        (gcnew FileIOPermission(FileIOPermissionAccess::Write, "C:\\Test"))->Demand();
      }
      catch(Exception^)
      {
        bnWriteFile->Enabled = false;
      }
        }

        System::Windows::Forms::Label^  lbOutput;
        System::Windows::Forms::Button^  bnWriteFile;
        System::Windows::Forms::Button^  bnReadFile;
        System::ComponentModel::Container ^components;

        void InitializeComponent(void)
        {
            this->lbOutput = (gcnew System::Windows::Forms::Label());
            this->bnWriteFile = (gcnew System::Windows::Forms::Button());
            this->bnReadFile = (gcnew System::Windows::Forms::Button());
            this->SuspendLayout();
            // 
            // lbOutput
            // 
            this->lbOutput->AutoSize = true;
            this->lbOutput->Location = System::Drawing::Point(6871);
            this->lbOutput->Name = L"lbOutput";
            this->lbOutput->Size = System::Drawing::Size(013);
            this->lbOutput->TabIndex = 5;
            // 
            // bnWriteFile
            // 
            this->bnWriteFile->Location = System::Drawing::Point(17030);
            this->bnWriteFile->Name = L"bnWriteFile";
            this->bnWriteFile->Size = System::Drawing::Size(7523);
            this->bnWriteFile->TabIndex = 4;
            this->bnWriteFile->Text = L"Write File";
            this->bnWriteFile->UseVisualStyleBackColor = true;
            this->bnWriteFile->Click += 
                gcnew System::EventHandler(this, &Form1::bnWriteFile_Click);
            // 
            // bnReadFile
            // 
            this->bnReadFile->Location = System::Drawing::Point(4830);
            this->bnReadFile->Name = L"bnReadFile";
            this->bnReadFile->Size = System::Drawing::Size(7523);
            this->bnReadFile->TabIndex = 3;
            this->bnReadFile->Text = L"Read File";
            this->bnReadFile->UseVisualStyleBackColor = true;
            this->bnReadFile->Click += 
                gcnew System::EventHandler(this, &Form1::bnReadFile_Click);
            // 
            // Form1
            // 
            this->AutoScaleDimensions = System::Drawing::SizeF(613);
            this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
            this->ClientSize = System::Drawing::Size(292110);
            this->Controls->Add(this->lbOutput);
            this->Controls->Add(this->bnWriteFile);
            this->Controls->Add(this->bnReadFile);
            this->Name = L"Form1";
            this->Text = L"CAS Security Test";
            this->ResumeLayout(false);
            this->PerformLayout();

        }

        System::Void bnReadFile_Click(System::Object^ sender, 
                                      System::EventArgs^ e
        {
            StreamReader ^sr = File::OpenText("C:\\Test\\TestFile.txt");
            String ^s = sr->ReadLine();
            sr->Close();
            lbOutput->Text = s;        
       }

        System::Void bnWriteFile_Click(System::Object^ sender, 
                                       System::EventArgs^ e
        {
            StreamWriter ^sw = File::CreateText("C:\\Test\\TestFile.txt");
            sw->WriteLine("This is a test. This is only a test.");
            sw->Close();
            lbOutput->Text = "Wrote text to file.";        
        }
    };
 

   
  
Related examples in the same category
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.