公司员工没有管理员权限怎么办 C#文件夹、文件添加权限 C#读取windows系统日志 C#设置环境变量
在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站点就可以访问了。
欢迎大家纠正
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构