3
Answers

Create Program to set an expiry of any uploaded content

Paras Solanki

Paras Solanki

9y
373
1
i have created one website for advertisement but when user upload images to my site i want to expiry on particular time for ex 30, 60, 90 days but i dont know how to perform that task and i m new in the asp.net i have started 2 months ago so please help me in this problem. i have make this website in c# asp.net
 
thanks and regards
paras solanki
Answers (3)
0
Scott Lysle

Scott Lysle

NA 28.5k 14.4m 16y

Per Ryan's suggestion:

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using System.Security.AccessControl;

 

 

namespace FileSecurity

{

    class Program

    {

        static void Main(string[] args)

        {

                        try

            {

                string fileName = "c:\\temp\\somefile.txt";

 

                Console.WriteLine("Adding access control entry for "

                    + fileName);

 

                // Add the access control entry to the file.

                AddFileSecurity(fileName, @"SomeFolder\ AnotherFolder",

                    FileSystemRights.Read, AccessControlType.Deny);

 

                // Add the access control entry to the file.

                AddFileSecurity(fileName, @"SomeFolder\AnotherFolder",

                    FileSystemRights.Write, AccessControlType.Deny);

 

                //Console.WriteLine("Removing access control entry from "

                //    + fileName);

 

                //// Remove the access control entry from the file.

                //RemoveFileSecurity(fileName, @"SomeFolder\AnotherFolder",

                //    FileSystemRights.ReadData, AccessControlType.Allow);

 

                Console.WriteLine("Done.");

                Console.Read();

            }

            catch (Exception e)

            {

                Console.WriteLine(e);

                Console.Read();

            }

        }

 

        // Adds an ACL entry on the specified file for the specified account.

        public static void AddFileSecurity(string fileName, string account,

            FileSystemRights rights, AccessControlType controlType)

        {

           

 

            // Get a FileSecurity object that represents the

            // current security settings.

            System.Security.AccessControl.FileSecurity fSecurity =

            File.GetAccessControl(fileName);

 

            // Add the FileSystemAccessRule to the security settings.

            fSecurity.AddAccessRule(new FileSystemAccessRule(account,

                rights, controlType));

 

            // Set the new access settings.

            File.SetAccessControl(fileName, fSecurity);

 

        }

 

        // Removes ACL entry on file for the specified account.

        public static void RemoveFileSecurity(string fileName, string   

        account, FileSystemRights rights, AccessControlType controlType)

        {

 

            // Get a FileSecurity object that represents the

            // current security settings.

            System.Security.AccessControl.FileSecurity fSecurity =

            File.GetAccessControl(fileName);

 

            // Add the FileSystemAccessRule to the security settings.

            fSecurity.RemoveAccessRule(new FileSystemAccessRule(account,

                rights, controlType));

 

            // Set the new access settings.

            File.SetAccessControl(fileName, fSecurity);

 

        }

 

    }

}

 

Accepted
0
Scott Lysle

Scott Lysle

NA 28.5k 14.4m 16y

Per Ryan's suggestion:

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using System.Security.AccessControl;

 

 

namespace FileSecurity

{

    class Program

    {

        static void Main(string[] args)

        {

                        try

            {

                string fileName = "c:\\temp\\somefile.txt";

 

                Console.WriteLine("Adding access control entry for "

                    + fileName);

 

                // Add the access control entry to the file.

                AddFileSecurity(fileName, @"SomeFolder\ AnotherFolder",

                    FileSystemRights.Read, AccessControlType.Deny);

 

                // Add the access control entry to the file.

                AddFileSecurity(fileName, @"SomeFolder\AnotherFolder",

                    FileSystemRights.Write, AccessControlType.Deny);

 

                //Console.WriteLine("Removing access control entry from "

                //    + fileName);

 

                //// Remove the access control entry from the file.

                //RemoveFileSecurity(fileName, @"SomeFolder\AnotherFolder",

                //    FileSystemRights.ReadData, AccessControlType.Allow);

 

                Console.WriteLine("Done.");

                Console.Read();

            }

            catch (Exception e)

            {

                Console.WriteLine(e);

                Console.Read();

            }

        }

 

        // Adds an ACL entry on the specified file for the specified account.

        public static void AddFileSecurity(string fileName, string account,

            FileSystemRights rights, AccessControlType controlType)

        {

           

 

            // Get a FileSecurity object that represents the

            // current security settings.

            System.Security.AccessControl.FileSecurity fSecurity =

            File.GetAccessControl(fileName);

 

            // Add the FileSystemAccessRule to the security settings.

            fSecurity.AddAccessRule(new FileSystemAccessRule(account,

                rights, controlType));

 

            // Set the new access settings.

            File.SetAccessControl(fileName, fSecurity);

 

        }

 

        // Removes ACL entry on file for the specified account.

        public static void RemoveFileSecurity(string fileName, string   

        account, FileSystemRights rights, AccessControlType controlType)

        {

 

            // Get a FileSecurity object that represents the

            // current security settings.

            System.Security.AccessControl.FileSecurity fSecurity =

            File.GetAccessControl(fileName);

 

            // Add the FileSystemAccessRule to the security settings.

            fSecurity.RemoveAccessRule(new FileSystemAccessRule(account,

                rights, controlType));

 

            // Set the new access settings.

            File.SetAccessControl(fileName, fSecurity);

 

        }

 

    }

}

 

0
Ryan Alford

Ryan Alford

NA 2.3k 891.7k 16y
you can set the permissions of the directory to not allow write access.  Then use code to programmatically change the permissions to allow the writing of the file, write the file, close the file, then restore the old permissions.
Next Recommended Forum