Tar*_*ied 3 c# windows-services
我无法在我的Windows服务中创建文件,这是错误的
错误在onstart方法中拒绝访问路径"C:\ Windows\system32\BridgeServiceLog.txt".
protected override void OnStart(string[] args)
{
try
{
Logger.InitLogFile("BridgeServiceLog.txt");
Trace.WriteLine(Logger.logSwitch.TraceInfo, "Trace Started");
Trace.WriteLineIf(Logger.logSwitch.TraceInfo, "OnStart Started");
_bridgeServiceEventLog.WriteEntry("new OnStart");
if (Vytru.Platform.Bridge.Configuration.LicenseValidetor.ValidCountAndTypeDevices())
{
SharedData.InitializeBridge();
// WsInitializeBridge();
}
else
{
this.Stop();
_bridgeServiceEventLog.WriteEntry("LicenseValidetor Error");
}
_bridgeServiceEventLog.WriteEntry("end Start");
}
catch (Exception e)
{
Trace.WriteLineIf(Logger.logSwitch.TraceError, e.Message);
_bridgeServiceEventLog.WriteEntry("error In onstart method " + e.Message);
}
Trace.WriteLineIf(Logger.logSwitch.TraceInfo, "OnStart Ended");
}
Run Code Online (Sandbox Code Playgroud)
服务用户帐户可能无权写入C:\Windows\System32(这是Windows服务的工作目录).
无论如何,你不应该写入该文件夹.它适用于操作系统 - 而不是您的服务.
您可以使用Environment.GetFolderPath获取合适的路径,以便以任何计算机(而不仅仅是您自己的计算机)的方式编写日志文件等文件.这是一个例子.
var companyPath = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData),
"MyCompany"
);
var productPath = Path.Combine(companyPath, "MyProduct");
var logFilePath = Path.Combine(productPath, "BridgeServiceLog.txt");
Run Code Online (Sandbox Code Playgroud)
你当然应该为MyCompany和使用合适的值MyProduct.
运行Windows服务时,默认工作文件夹是<System drive>:\Windows\System32\.
幸运的是,不是每个人都可以访问该文件夹.
这有两种方式; 将您的文件写入您拥有权限的另一个文件夹,或使用管理员权限运行您的服务.
我会推荐第一个选项.
| 归档时间: |
|
| 查看次数: |
8560 次 |
| 最近记录: |