3
Answers

How to protect xml file

 

I have a windows application in C#.net.In my application there is a xml file which is updated on every 30 seconds by the application automatically.I don't want the external user to update or tamper the xml file.So how can I protect the xml file so that other users can not read or update the xml file. I want the xml file to be updated automatically by the Application only.


Help me.

Thanks in advance.
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.