以下是我的代码(C# windowsForm)。当我发布我的项目时,在其他计算机上出现错误/异常:
The type initializer for 'OlampiadEbtedaee.NeedFunction' threw an exception.
我在主窗体加载事件中调用此类的函数 (NeedFunction)。问题可能是因为我使用的是 microsoft.win32 吗??还是其他电脑访问registery失败??
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.Win32;
using System.IO;
namespace OlampiadEbtedaee
{
public static class NeedFunctions
{
public static bool IsStringInFile(string fileName, string searchString)
{
return File.ReadAllText(fileName).Contains(searchString);
}
////////////////////////////////////////
public static string TaskbarsizeState()
{
RegistryKey myKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced", false);
String value = myKey.GetValue("TaskbarSizeMove").ToString();
return value;
}
public static string PaintStatusBarState()
{
RegistryKey myKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Applets\paint\view", false);
String value = myKey.GetValue("ShowStatusBar").ToString();
return value;
}
public static string magnifierState()
{
RegistryKey myKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\ScreenMagnifier", false);
String value = myKey.GetValue("ZoomIncrement").ToString();
return value;
}
public static string taskbarSizeMove = TaskbarsizeState();
public static string paintStatusBarState = PaintStatusBarState();
public static string getMagnifierZoomState = magnifierState();
public static string GetPathOfWallpaper()
{
string pathWallpaper = "";
RegistryKey regKey = Registry.CurrentUser.OpenSubKey("Control Panel\\Desktop", false);
if (regKey != null)
{
pathWallpaper = regKey.GetValue("WallPaper").ToString();
regKey.Close();
}
return pathWallpaper;
}
public static void CreatePrequires()
{
Directory.CreateDirectory(@"D:\D");
Directory.Delete(@"D:\D");
Directory.CreateDirectory(@"D:\F");
Directory.CreateDirectory(@"D:\X");
}
}
您的问题是您没有检查注册表项是否存在,也没有访问它们的值。先创建注册表项,然后运行你的项目,你的问题就会解决
我是一名优秀的程序员,十分优秀!