在xx工作快一年了,像xx这样的一些大公司 一般用户都没有管理员权限是一个头疼的事情。其实一般情况下页没什么,有时候还真是比较麻烦。不过xx自己有一个播发程序 里面打开的程序都具有管理员权限,如vs2010,sql2008,利用这个播发程序可以安装某些软件。
对于开发人员播发程序不太够啊,所以很多时候可以写C#代码来实现
例如 安装软件 Process.Start(path);
删除文件夹
void DeleteDirectory(string path)
{
DirectoryInfo dir = new DirectoryInfo(path);
foreach (var item in dir.GetDirectories())
{
item.Delete(true);
}
dir.Delete(true);
}
设置环境变量
/// <summary>
/// 设置windows环境变量
/// </summary>
/// <param name="name">变量名称</param>
/// <param name="value">变量值</param>
public static void SetEnvironmentVariable(string name, string value)
{
RegistryKey regLocalMachine = Registry.LocalMachine;
RegistryKey regSYSTEM = regLocalMachine.OpenSubKey("SYSTEM", true);//打开HKEY_LOCAL_MACHINE下的SYSTEM
RegistryKey regControlSet001 = regSYSTEM.OpenSubKey("ControlSet001", true);
RegistryKey regControl = regControlSet001.OpenSubKey("Control", true);
RegistryKey regManager = regControl.OpenSubKey("Session Manager", true);
RegistryKey regEnvironment = regManager.OpenSubKey("Environment", true);
regEnvironment.SetValue(name, value);
}
给文件夹、文件添加权限
// <summary>
/// Adds an ACL entry on the specified directory for the specified account.
/// This function was taken directly from MSDN. It adds security rights to a folder
/// </summary>
/// <param name="FileName"></param>
/// <param name="Account">like @"BUILTIN\Administrators" or @"BUILTIN\Users" </param>
/// <param name="Rights">like FileSystemRights.FullControl</param>
/// <param name="ControlType">like AccessControlType.Allow</param>
///
public static void AddDirectorySecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
{
// Create a new DirectoryInfo object.
DirectoryInfo dInfo = new DirectoryInfo(FileName);
// Get a DirectorySecurity object that represents the
// current security settings.
DirectorySecurity dSecurity = dInfo.GetAccessControl();
// Add the FileSystemAccessRule to the security settings.
dSecurity.AddAccessRule(new FileSystemAccessRule(Account, Rights, ControlType));
// Set the new access settings.
dInfo.SetAccessControl(dSecurity);
}
/// <summary>
/// Adds an ACL entry on the specified directory for the specified account.
/// This function was taken directly from MSDN. It adds security rights to a file
/// </summary>
/// <param name="FileName"></param>
/// <param name="Account">like @"BUILTIN\Administrators" or @"BUILTIN\Users" </param>
/// <param name="Rights">like FileSystemRights.FullControl</param>
/// <param name="ControlType">like AccessControlType.Allow</param>
public static void AddFileSecurity(string FileName, string Account, FileSystemRights Rights, AccessControlType ControlType)
{
// Create a new FileInfo object.
FileInfo fInfo = new FileInfo(FileName);
// Get a FileSecurity object that represents the
// current security settings.
FileSecurity fSecurity = fInfo.GetAccessControl();
// Add the FileSystemAccessRule to the security settings.
fSecurity.AddAccessRule(new FileSystemAccessRule(Account,
Rights,
ControlType));
// Set the new access settings.
fInfo.SetAccessControl(fSecurity);
}
读取windows日志
/// <summary>
/// 读取windows 系统日志
/// </summary>
/// <returns></returns>
public static string ReadWindowsLog()
{
string[] logs = new string[] { "Application", "System", "Security" };
/*清空所有日志*/
//EventLog eventlog = new EventLog();
//foreach (var item in logs)
//{
// eventlog.Log = item;
// eventlog.Clear();
//}
/*清空所有日志*/
StringBuilder sb = new StringBuilder();
foreach (string log in logs)
{
EventLog myLog = new EventLog();
myLog.Log = log;
//myLog.MachineName = "rondi-agt0qf9op";
foreach (EventLogEntry entry in myLog.Entries)
{
//EventLogEntryType枚举包括:
//Error 错误事件。
//FailureAudit 失败审核事件。
//Information 信息事件。
//SuccessAudit 成功审核事件。
//Warning 警告事件。
if (entry.EntryType == EventLogEntryType.Error || entry.EntryType == EventLogEntryType.Warning)
{
sb.Append(log);
sb.Append(entry.EntryType.ToString());
sb.Append(entry.TimeWritten.ToString());
sb.Append(entry.Message + "\r\n");
}
}
}
return sb.ToString();
}
对与我们开发人员,读取日志和给文件、文件夹添加权限是很重要的。这天帮同事在计算机上配置了一个iis站点,不站点为什么已访问该站点相应的应用程序池 就停止了,我们怀疑于操作系统有关。可是不能查看日志,最后只有把it叫过来查看日志,发现什么文件权限不过,再添加权限iis站点就可以访问了。
欢迎大家纠正