Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > Other .NET > General .NET
|
General .NET For general discussion of MICROSOFT .NET topics that don't fall within any of the other .NET forum subcategories or .NET language forums.  If your question is specific to a language (C# or Visual Basic) or type of application (Windows Forms or ASP.Net) try an applicable forum category. ** PLEASE BE SPECIFIC WITH YOUR QUESTION ** When posting here, provide details regarding the Microsoft .NET language you are using and/or what type of application (Windows/Web Forms, etc) you are working in, if applicable to the question. This will help others answer the question without having to ask.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the General .NET section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 10th, 2007, 09:30 PM
Authorized User
 
Join Date: Dec 2006
Posts: 26
Thanks: 0
Thanked 0 Times in 0 Posts
Default Creating a NewFolder using c#

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.O rganizationName,RootSystem.Bills.ContactID,
                RootSystem.Bills.Address,RootSystem.Bills.BatchID, RootSystem.Bills.ChargedDate )
                .Where(RootSystem.Bills.IsBilled==0));

            string BatchID="Bill"+DateTime.Now.Day.ToString()+DateTim e.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.Rows[i][j].ToString() + "\t");
                    }
                    str.Append("\r\n");
                }
                string filename=logPath+BatchID+".txt";
                StreamWriter writer = new StreamWriter(filename);
                writer.WriteLine(str.ToString());
                writer.Close();
__________________
Karthik
[Nothing is impossible]
 
Old January 10th, 2007, 09:45 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

hi srkarthik,

eg.
Code:
if (!Directory.Exists(@"C:/Temp"))
{
   Directory.CreateDirectory(@"C:/Temp");
}
or in your case Directory.Exists(logPath);





======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
 
Old January 11th, 2007, 03:42 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

Just to be difficult:

srkarthik_82, I installed windows on the D: drive and I don't have a C: drive. Your program doesn't run.

If you want to work with temporary directories and files, use the methods provided by .NET for it:

Returns a unique temporary filename (also creates a zero-length file on the disk)

   System.IO.Path.GetTempFileName()

Returns the path to the system's temporary folder

   System.IO.Path.GetTempPath()

-Peter
 
Old January 11th, 2007, 09:52 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Yeah, I agree.

I was really answering:
 
Quote:
quote:If there is no folder called Temp in c:,then i want to create a new folder[Temp] dynamically.


The Folder location is stored in his app.config, and this enables the app to use any folder, which I think is a bonus.

However, if the required folder is not on the root drive the code will have to be amended to test and create all folder in the path.








======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================





Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating New Database everest SQL Server 2005 2 July 10th, 2007 01:32 PM
Creating Menus Andrew_S ASP.NET 2.0 Basics 1 May 7th, 2006 12:58 PM
creating tooltips? Hannibal Javascript How-To 6 April 27th, 2006 09:20 AM
Creating Files xchemicalbeatz VB.NET 2002/2003 Basics 6 April 14th, 2005 08:24 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.