NetC常用系统操作获取系统文件设置开机启动等
获取常用系统文件目录static void Main(string[] args){ //1、通过Environment.GetFolderPath()获取 string pathFavorites = Environment.GetFolderPath(Environment.SpecialFolder.Favorites);//获取我的收藏路径 Console.WriteLine("Favorites:" + pathFavorites); string pathDesktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);//获取桌面路径 Console.WriteLine("Desktop:" + pathDesktop); string pathMyDocuments = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);//获取我的文档路径 Console.WriteLine("MyDocuments:" + pathMyDocuments); string pathMyPictures = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);//获取我的图片路径 Console.WriteLine("MyPictures:" + pathMyPictures); string pathMyVideos = Environment.GetFolderPath(Environment.SpecialFolder.MyVideos);//获取我的视频路径 Console.WriteLine("MyVideos:" + pathMyVideos); string pathDownloads = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\Downloads";//获取下载路径(这个没有找到,所以需要配合UserProfile来拼接了) Console.WriteLine("Downloads:" + pathDownloads); //2、通过环境变量获取 string tempPath = Environment.GetEnvironmentVariable("TEMP"); Console.WriteLine("TEMP:" + tempPath); Console.ReadKey();}设置开机启动private string registryName = "BootTestDemo";//启动项的名称private void btnSubmit_Click(object sender, EventArgs e){ if (this.ckBootEntry.Checked) { string filePath = Environment.CurrentDirectory + "\\PowerBootDemo.exe"; SetBoot(this.registryName, filePath); } else { DeleteBoot(this.registryName); } MessageBox.Show("Success");}/// <summary>/// 检测注册项是否存在/// </summary>/// <param name="key"></param>/// <returns></returns>private bool CheckExisting(string key){ string[] allNames = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run").GetValueNames(); foreach (var item in allNames) { if (item.Equals(key)) { return true; } } return false;}/// <summary>/// 设置启动项/// </summary>private void SetBoot(string key, string path){ if (CheckExisting(this.registryName)) return;//检测是否存在 Microsoft.Win32.RegistryKey registryKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run");//在系统启动的注册表中创建子项 registryKey.SetValue(key, path);//为子项进行赋值}/// <summary>/// 删除启动项/// </summary>public void DeleteBoot(string key){ if (!CheckExisting(this.registryName)) return;//检测是否存在 Microsoft.Win32.RegistryKey registryKey = Microsoft.Win32.Registry.CurrentUser.CreateSubKey(@"Software\Microsoft\Windows\CurrentVersion\Run"); registryKey.DeleteValue(key, false);}//PS:如果记不住这么长的路径可以直接打开系统注册表编辑器来翻看(cmd执行:regedit)。持续更新.... ...