2012-09-26

Change access right on directory for Users group or current user

If you need set some access right to specific folder I can get idea from this code how to do that.



    public static void AllowModifyOnDirectory(string directoryPath)
    {
      ifstring.IsNullOrWhiteSpacedirectoryPath ) || !Directory.ExistsdirectoryPath )  )
      {
        return;
      }
 
      try
      {
        // Get the access rules of the specified files (user groups and user names that have access to the directory)
        var rules = File.GetAccessControl(directoryPath).GetAccessRules(truetruetypeof (SecurityIdentifier));
 
        // Get the identity of the current user and the groups that the user is in.
        var groups = WindowsIdentity.GetCurrent().Groups;
        string sidCurrentUser = WindowsIdentity.GetCurrent().User.Value;
 
        // Check if modifieng is allowed
        bool allowModify =
          rules.OfType<FileSystemAccessRule>().Any(
            rule =>
            (groups.Contains(rule.IdentityReference|| rule.IdentityReference.Value == sidCurrentUser&&
            rule.AccessControlType == AccessControlType.Allow &&
            (rule.FileSystemRights & FileSystemRights.Modify== FileSystemRights.Modify);
 
 
        if (allowModify)
        {
          return;
        }
 
        
        DirectoryInfo di = new DirectoryInfo(directoryPath);
        DirectorySecurity directorySecurity = di.GetAccessControl();
        bool modified;
        
        try
        {
          // Try to add modify rights to all users in Users 
          FileSystemAccessRule fileSystemAccessRule = new FileSystemAccessRule("Users"FileSystemRights.Modify,
                                                                               AccessControlType.Allow);
          directorySecurity.ModifyAccessRule(AccessControlModification.SetfileSystemAccessRuleout modified);
        }
        catch
        {
          // Try to add modify rights to curent user
          string identity = Environment.UserDomainName + "\\" + Environment.UserName;
          FileSystemAccessRule fileSystemAccessRule = new FileSystemAccessRule(identityFileSystemRights.Modify,
                                                                               AccessControlType.Allow);        
          directorySecurity.ModifyAccessRule(AccessControlModification.SetfileSystemAccessRuleout modified);
        }
      }
      catch
      { }
 
    }

2012-01-26

How to execute an .SQL script file using C#

If you need to execute TSQL script inside C# code using only standard libs then you can use this.





FileInfo file = new FileInfo(@"E:\someSqlScript.sql");
string script = file.OpenText().ReadToEnd();


  // split script on GO command
  IEnumerable<string> commandStrings = Regex.Split( script, "^\\s*GO\\s*$", RegexOptions.Multiline );

  Connection.Open();
  foreach( string commandString in commandStrings )
  {
    if( commandString.Trim() != "" )
    {
      new SqlCommand( commandString, Connection ).ExecuteNonQuery();
    }
  }     
  Connection.Close();

Generate random number in Oracle

Sometimes you need unique random number. Here is code for PL/SQL



FUNCTION GetRandNumber RETURN Number IS

 l_seed BINARY_INTEGER;
 l_random_num NUMBER(5);
 l_date VARCHAR2(25);
 l_random VARCHAR2(4);

BEGIN
  l_seed := TO_NUMBER(TO_CHAR(SYSTIMESTAMP, 'FF'));
  DBMS_RANDOM.initialize (val => l_seed);
  l_random_num := TRUNC(DBMS_RANDOM.value(low => 1, high => 65535));
  DBMS_RANDOM.terminate;
  return l_random_num;


END GetRandNumber;