如何将日志文件写入当前目录路径。 [英] How to write a log file to the current directory path.

查看:305
本文介绍了如何将日志文件写入当前目录路径。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如何将日志文件写入当前目录路径。

我有以下代码将文件写入特定位置。我需要更改代码,以便将文件写入当前目录。请帮助我。

Hi,
How to write a log file to the current directory path.
I have the following code to write file to a specific location. I need to change the code in such a way that the file should be written to the current directory. Please help me.

public void WriteFile(string message, byte[] respMsg)
       {
           FileStream fileStream = null;
           StreamWriter streamWriter = null;
           try
           {
               string currentDir = Environment.CurrentDirectory;
               DirectoryInfo directory = new DirectoryInfo(currentDir);
               FileInfo file = new FileInfo(args[0]);


               string logFilePath = "../../LogError\\";

               logFilePath = logFilePath + "ProgramLog" + "-" + DateTime.Today.ToString("yyyyMMdd") + "." + "txt";

               if (logFilePath.Equals("")) return;
               #region Create the Log file directory if it does not exists
               DirectoryInfo logDirInfo = null;
               FileInfo logFileInfo = new FileInfo(logFilePath);
               logDirInfo = new DirectoryInfo(logFileInfo.DirectoryName);
               if (!logDirInfo.Exists) logDirInfo.Create();
               #endregion Create the Log file directory if it does not exists

               if (!logFileInfo.Exists)
               {
                   fileStream = logFileInfo.Create();
               }
               else
               {
                   fileStream = new FileStream(logFilePath, FileMode.Create);
               }
               streamWriter = new StreamWriter(fileStream);
               streamWriter.WriteLine(message);
               StringBuilder builder = new StringBuilder(respMsg.Length * 3);
               foreach (byte data in respMsg)
                   builder.Append(Convert.ToString(data, 16).PadLeft(2, '0').PadRight(3, ' '));
               streamWriter.WriteLine(builder.ToString().Trim());

           }

推荐答案

通过指定File方法的无路径,当前目录将会被使用;当前目录目录是指应用程序所在的目录。



源代码应为:

By specifying no path for "File" methods, the current directory will be used; and current directory directory means the directory where your application is located.

The source code should be:
public void WriteFile(string message, byte[] respMsg)
        {
            FileStream fileStream = null;
            StreamWriter streamWriter = null;
            try
            {
                string logFileName ="ProgramLog" + "-" + DateTime.Today.ToString("yyyyMMdd") + "." + "txt";
 
                fileStream = new FileStream(logFileName, FileMode.Create);
                // 
                streamWriter = new StreamWriter(fileStream);
                streamWriter.WriteLine(message);
                StringBuilder builder = new StringBuilder(respMsg.Length * 3);
                foreach (byte data in respMsg)
                    builder.Append(Convert.ToString(data, 16).PadLeft(2, '0').PadRight(3, ' '));
                streamWriter.WriteLine(builder.ToString().Trim());
 
            }
            //...


string currentDir = Environment.CurrentDirectory;
                 string logFilePath = currentDir+"\\LogError\\";
                Console.WriteLine("==="+logFilePath);

                logFilePath = logFilePath + "ProgramLog" + "-" + DateTime.Today.ToString("yyyyMMdd") + "." + "txt";

 fileStream = new FileStream(logFilePath, FileMode.CreateNew);
 streamWriter = new StreamWriter(fileStream);
               streamWriter.WriteLine(message);


这篇关于如何将日志文件写入当前目录路径。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆