2013-07-19 2 views
1

어떻게 txt로 출력 할 수 있습니까? 하지만 경우에이 같은C#에서 txt로 출력 클래스 라이브러리

public class ProjectHandler:Microsoft.Office.Project.Server.Events.ProjectEventReceiver { 

} 

public static void WriteToEventLog(string textLog, EventLogEntryType logtype) { 
    EventLog eventlog = new EventLog(); 
    eventlog.Source = "Project Event Handler"; 
    eventlog.WriteEntry(logtype.ToString() + ":" + textLog, logtype); 

} 

public override void OnDeleting(PSContextInfo contextInfo, ProjectPreEventArgs e) { 

    WriteToEventLog(string.Format("Пользователь \"{0}\" удалил проект \"{1}\"", contextInfo.UserName, e.ProjectName), EventLogEntryType.Information); 

    base.OnDeleting(contextInfo, e); 
} 
+0

서식을 신중히 선택하십시오. http://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks 및 http://stackoverflow.com/editing-help를 참조하십시오. –

답변

1

public static void WriteToTextFile(string textLog) 
{ 
    FileStream objFS = null; 


    string strFilePath = AppDomain.CurrentDomain.BaseDirectory + @"\Exception Log\" + System.DateTime.Now.ToString("yyyy-MM-dd ") + "Exception.log"; 
    if (!File.Exists(strFilePath)) 
    { 
      objFS = new FileStream(strFilePath, FileMode.Create); 
    } 
    else 
      objFS = new FileStream(strFilePath, FileMode.Append); 

    using (StreamWriter Sr = new StreamWriter(objFS)) 
    { 
     Sr.WriteLine(System.DateTime.Now.ToShortTimeString() + "---" + textLog); 
     } 

} 

다음

WriteToEventLog(string.Format("Пользователь \"{0}\" удалил проект \"{1}\"", contextInfo.UserName, e.ProjectName), EventLogEntryType.Information); 

WriteToTextFile(string.Format("Пользователь \"{0}\" удалил проект \"{1}\"", contextInfo.UserName, e.ProjectName)); 
+0

대단히 감사합니다! –

0
public override void OnDeleting(PSContextInfo contextInfo, ProjectPreEventArgs e) 
    { 
     using (System.IO.StreamWriter sw = new System.IO.StreamWriter(@"C:\Blah.txt")) 
     { 
      string textToWrite = string.Format("Пользователь \"{0}\" удалил проект \"{1}\"", contextInfo.UserName, e.ProjectName); 
      sw.WriteLine(textToWrite); 
     } 
    } 

뭔가를 기록합니다. StreamWriter 개체를 사용하십시오.

EDIT 분명히 C : \ 드라이브 등에 쓸 수있는 권한이나 쓰기를 원하는 곳이면 어디든 있습니다. 대신 이벤트에 쓰기

0

사용이 줄을 변경 추가를 텍스트 파일

에이 방법을 작성해야 로그인 File.WriteAllText 방법.

string path = @"c:\temp\MyTest.txt"; 
string createText = "Hello and Welcome" + Environment.NewLine; 
File.WriteAllText(path, createText, Encoding.UTF8);