公司员工没有管理员权限怎么办 C#文件夹、文件添加权限 C#读取windows系统日志 C#设置环境变量
2012-08-15 10:05
1031 查看
在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站点就可以访问了。
欢迎大家纠正
对于开发人员播发程序不太够啊,所以很多时候可以写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站点就可以访问了。
欢迎大家纠正
相关文章推荐
- 公司员工没有管理员权限怎么办 C#文件夹、文件添加权限 C#读取windows系统日志 C#设置环境变量
- 以Android L读取系统所有logcat并写入文件为例分析Android 以添加系统进程的方式申请selinux的权限执行shell脚本,以及avc:dined应该怎么申请权限
- 设置C#程序在Windows 7 Vista下以管理员权限运行(转)
- C#修改文件或文件夹的权限,为指定用户、用户组添加完全控制权限
- C#修改文件或文件夹的权限,为指定用户、用户组添加完全控制权限
- 设置C#程序在Windows 7 Vista下以管理员权限运行(转)
- C# 如何在 Windows 文件系统中添加自定义右键菜单
- 在virtualbox中,安装redhat系统,发现在/mnt下面,没有虚拟机设置的share文件夹,如何共享文件呢
- Windows系统没有的文件或文件夹
- Windows系统中使用C#读取文本文件内容的小示例
- C#修改文件或文件夹的权限,为指定用户、用户组添加完全控制权限
- windows 8/10 文件、文件夹需要管理员权限处理办法
- 读取WEB-INF下的日志文件并添加到集合(windows和linux环境通用)
- 如何设置C#程序在Windows 7 Vista下以管理员权限运行
- windows在哪里添加右键管理员权限?怎么添加?
- Windows 7提示没有管理员权限怎么办
- C#添加文件和文件夹访问用户并设定权限
- C#修改文件或文件夹的权限,为指定用户、用户组添加完全控制权限
- C#添加文件和文件夹访问用户并设定权限
- virtualbox windows 主机 安装ubuntu系统 共享文件夹 没有权限访问