Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
635 views
in Technique[技术] by (71.8m points)

events - Anyone know why I can't access the winevt folder programmatically in C#?

I've been trying to get into the 'C:WindowsSystem32winevtLogs' folder programmatically using C# so I can copy the event log files to a backup directory and then clear the event logs as a part of a daily backup apparatus, but I don't seem to be able to get access to this directory.

I've tried changing the application manifest to run under administrator ( ) which gives me the UAC prompt when I execute the program and I've even gone as far as to spawn a shell under NT AUHORITYSYSTEM identity to execute the code but it still says it's an invalid path, even though I can manually go into the directory under both administrative shell and the SYSTEM shell.

I've isolated it to just not being able to go into the winevt dir. I use this code to see if I can access the directory.

Environment.CurrentDirectory = System.Environment.SystemDirectory + @"winevt";

only to receive

System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:Windowssystem32winevt'. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.Directory.SetCurrentDirectory(String path) at System.Environment.set_CurrentDirectory(String value) at dev_EventLog.Program.Main(String[] args) in D:SourceCodesdev_EventLogdev_EventLogProgram.cs:line 30

I've tried many different ways to specify the directory but it's all the same, and I've also tried different subfolder of System32 and of the 10 or so I tried winevt is the only one to act like this.

This has been driving me nuts, anyone know why this isn't working under C# or am I forced to use VBScript to do this, since the following VBScript code works to copy the event log file.

dim filesys set filesys=CreateObject("Scripting.FileSystemObject") filesys.CopyFile "C:WindowsSystem32winevtLogsApplication.evtx", "C: uslApplication.evtx"

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Is your application running as a 32-bit application on a 64-bit version of Windows? If so, any access to %windir%System32 is redirected to %windir%SystemWOW64 (where there is no winevt directory).

If you use %windir%Sysnativewinevt you should be able to access it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...