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

amitsinghr
 
on Sep 18, 2017 06:23 AM
10991 Views

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
Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Sep 18, 2017 06:25 AM

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.

amitsinghr
 
on Sep 20, 2017 02:04 AM

 

 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();
                }
            }     
        }