Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.
C#
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());

           }
Posted
Updated 28-Aug-14 18:57pm
v3
Comments
syed shanu 29-Aug-14 1:07am    
Current Directory path means Do you want to store the log file in your BIN folder.

string Current_directory_path = Path.GetDirectoryName(Path.GetDirectoryName(System.IO.Directory.GetCurrentDirectory()));
or
Current_directory_path = System.IO.Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );

or
Path.GetDirectoryName(Application.ExecutablePath)
Bernhard Hiller 29-Aug-14 2:41am    
So what's wrong with your code in your opinion?
Did you get some exception ("Access denied")?
Do you understand when Environment.CurrentDirectory and Application.StartupPath are different - and why it can be so?

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:
C#
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());
 
            }
            //...
 
Share this answer
 
v3
C#
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);
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900