Create folder inside bin directory using C# in ASP.Net

Last Reply one year ago By amitsinghr

Posted one year ago

IN MY CODE 

 

private void WriteToFile(string text)
{
string path = AppDomain.CurrentDomain.BaseDirectory + "\\line Log";
// string path = System.Reflection.Assembly.GetEntryAssembly().Location;
string logFile = "lineSearchAppLogFile" + DateTime.Now.ToString("ddMMyyyy") + ".txt";
string filePath = path+"\\"+logFile;
try
{
if (!Directory.Exists(path))
{ 
DirectoryInfo di = Directory.CreateDirectory(path);
}
else
{
if (!System.IO.File.Exists(path +"\\log"+DateTime.Now.ToString("ddMMyyyy")+".txt"))
{
// System.IO.File.Create(path + "\\log" + DateTime.Now.ToString("ddMMyyyy") + ".txt");
}
else
{
using (StreamWriter writer = new StreamWriter(path + "\\log" + DateTime.Now.ToString("ddMMyyyy") + ".txt", true))
{
writer.WriteLine(string.Format(text, DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt")));
writer.Close();
}
}
}
}
catch (IOException ioex)
{
Console.WriteLine(ioex.Message);
}

//string logFileName = "lineAppServiceLog.txt";
//using (StreamWriter writer = new StreamWriter(path, true))
//{
// writer.WriteLine(string.Format(text, DateTime.Now.ToString("dd/MM/yyyy hh:mm:ss tt")));
// writer.Close();
//}
}

in code 

AppDomain.CurrentDomain.BaseDirectory = i am getting location D:\\Project\\abc\\abc.line.Platform.lineServices\\bin\\Debug\\

and problem is every time when i rebuild project my folder go delete
so how to make folder outside
Debug folder in window service
Posted one year ago

Hi amitsinghr,

The folders inside the bin folder of your project are always deleted when you clean / rebuild your solution.

The simplest way of doing it is by adding the desired folder to your project.


Posted one year ago

 

 private void LogService(string content)
        {
            string path = AppDomain.CurrentDomain.BaseDirectory.Substring(0, AppDomain.CurrentDomain.BaseDirectory.Substring(0, AppDomain.CurrentDomain.BaseDirectory.LastIndexOf('\\')).LastIndexOf('\\')) + "\\ServiceLog";

            if (!Directory.Exists(path))
            {
                DirectoryInfo di = Directory.CreateDirectory(path);
            }

            string filePath = path + "\\log" + DateTime.Now.ToString("ddMMyyyy") + ".txt";

            using (FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite))

            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    sw.BaseStream.Seek(0, SeekOrigin.End);
                    sw.WriteLine(content);
                    sw.Flush();
                    sw.Close();
                }
            }     
        }