Creating a NewFolder using c#  
Author Message
karthik.sr





PostPosted: .NET Base Class Library, Creating a NewFolder using c# Top

Hi,

I am writting a textfile to c:\ Temp folder.
I want to check whether there is Temp folder available in c: , before i save the files
in c:/Temp.If there is no folder called Temp in c:,then i want to create a new folder[Temp]
dynamically.

Can somebody give me an suggetion to do it

My existing code is:

public void TextWrite()
{
string logPath = (string) (new System.Configuration.AppSettingsReader()).GetValue( "DataAccessLogPath", typeof(string));

////////////////////"DataAccessLogPath" is <add key="DataAccessLogPath" value="c:\Temp\"></add> given in App.Config file.


DataTable dt= Context.QueryDataTable(Sql.Select( RootSystem.Bills.WOWJ_NO,
RootSystem.Bills.AssetID,RootSystem.Bills.SuiteID,RootSystem.Bills.LeaseID,
RootSystem.Bills.OrganizationID,RootSystem.Bills.OrganizationName,RootSystem.Bills.ContactID,
RootSystem.Bills.Address,RootSystem.Bills.BatchID,RootSystem.Bills.ChargedDate )
.Where(RootSystem.Bills.IsBilled==0));

string BatchID="Bill"+DateTime.Now.Day.ToString()+DateTime.Now.Month.ToString ()+DateTime.Now.Year.ToString () ;

StringBuilder str = new StringBuilder();


for(int i=0;i<=dt.Rows.Count - 1; i++)
{
for(int j=0;j<=dt.Columns.Count - 1; j++)
{
if(dt.Columns[j].ColumnName.ToString ()=="BatchID")
str.Append(BatchID.ToString() + "\t");
else
str.Append(dt.RowsIdea[j].ToString() + "\t");
}
str.Append("\r\n");
}
string filename=logPath+BatchID+".txt";
StreamWriter writer = new StreamWriter(filename);
writer.WriteLine(str.ToString());
writer.Close();



.NET Development9  
 
 
Mark Dawson





PostPosted: .NET Base Class Library, Creating a NewFolder using c# Top

Hi,

you can use the DirectoryInfo class http://msdn2.microsoft.com/en-us/library/system.io.directoryinfo.aspx which has a method called Exists() which you can use to test for the existance of a directory. You can also use the Create() method to create a new directory.

Mark.



 
 
karthik.sr





PostPosted: .NET Base Class Library, Creating a NewFolder using c# Top

Thanks a lot Mark Dawson

I finished the task.............

code i used:

protected override void OnStart(string[] args)

{

string logPath = (string) (new System.Configuration.AppSettingsReader()).GetValue( "DataAccessLogPath", typeof(string));

//Checking for the folder

if(!Directory.Exists(logPath))

{

//Creating New Folder

Directory.CreateDirectory(logPath);

}

// TODO: Add code here to start your service.


 
 
RizwanSharp





PostPosted: .NET Base Class Library, Creating a NewFolder using c# Top

Thanks a lot Mark Dawson

I finished the task.............

code i used:

protected override void OnStart(string[] args)

{

string logPath = (string) (new System.Configuration.AppSettingsReader()).GetValue( "DataAccessLogPath", typeof(string));

//Checking for the folder

if(!Directory.Exists(logPath))

{

//Creating New Folder

Directory.CreateDirectory(logPath);

}

// TODO: Add code here to start your service.

Sure this will work like a Charm.

Best Regards and Bets Wishes,

Rizwan aka RizwanSharp



 
 
ShellShock





PostPosted: .NET Base Class Library, Creating a NewFolder using c# Top

You can also use System.IO.Path.GetTempPath to get the temporary directory for the current user (which will always exists). This is the Microsoft recommended method rather using your own temporary directory.