Link to home
Start Free TrialLog in
Avatar of raviLearning
raviLearning

asked on

Set a file/folder permission using asp .net

I have a situation where I need to create a folder from my application in the file system and set its permission from the code itself.

Thanks
Ravi
Avatar of Murtie
Murtie


Hi,

One way of doing this in Vb .NET

imports System.IO

public class MainClass
   Shared Sub Main()

File.Create("d:\test.tx")
        Dim oFp As FileIOPermission = New _
        FileIOPermission(FileIOPermissionAccess.Write, "d:\test.txt")
        oFp.Assert()
        Try
            Dim objWriter As New IO.StreamWriter(File.Open("d:\test.txt", IO.FileMode.Open))
            objWriter.WriteLine("Hi there!")
            objWriter.Flush()
            objWriter.Close()
            objWriter = Nothing
        Catch objA As System.Exception
            Console.WriteLine(objA.Message)
        End Try

   End Sub
End Class

Avatar of raviLearning

ASKER

I was looking for a solution using ASP .net
Your ASP .NET has either a c# or a vb .net codeFile or on the same file in <% %> brackets:

I have created an example below, hope it helps.

Murtie
' VB CODE Default.vb
Imports System.io
Imports System.Security.Permissions
 
 
Partial Class _Default
    Inherits System.Web.UI.Page
 
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
 
 
        File.Create(Me.Server.MapPath("~/Files/") & "test.txt")
        Dim oFp As System.Security.Permissions.FileIOPermission = New _
        FileIOPermission(FileIOPermissionAccess.Write, Me.Server.MapPath("~/Files/") & "test.txt")
        oFp.Assert()
        Try
            Dim objWriter As New IO.StreamWriter(File.Open(Me.Server.MapPath("~/Files/") & "test.txt", IO.FileMode.Open))
            objWriter.WriteLine("Hi there!")
            objWriter.Flush()
            objWriter.Close()
            objWriter = Nothing
        Catch objA As System.Exception
            Console.WriteLine(objA.Message)
        End Try
 
 
 
    End Sub
End Class
 
' ASP PAGE Default.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" /></div>
    </form>
</body>
</html>

Open in new window

Thanks For you comments,

But my requirements was for defining permissions for a folder from the code.
Here you go:


:o)
' VB CODE Default.vb
Imports System.io
Imports System.Security.Permissions
 
 
Partial Class _Default
    Inherits System.Web.UI.Page
 
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
 
 
        Directory.CreateDirectory(Me.Server.MapPath("~/YOURFOLDERNAME/"))
        Dim oFp As System.Security.Permissions.FileIOPermission = New _
        FileIOPermission(FileIOPermissionAccess.Write, Me.Server.MapPath("~/YOURFOLDERNAME/") )
        oFp.Assert()
        Try
            Dim objWriter As New IO.StreamWriter(File.Open(Me.Server.MapPath("~/Files/") & "test.txt", IO.FileMode.Open))
            objWriter.WriteLine("Hi there!")
            objWriter.Flush()
            objWriter.Close()
            objWriter = Nothing
        Catch objA As System.Exception
            Console.WriteLine(objA.Message)
        End Try
 
 
 
    End Sub
End Class
 
' ASP PAGE Default.aspx
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="Button1" runat="server" Text="Button" /></div>
    </form>
</body>
</html>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Murtie
Murtie

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial