10 Replies - 1952 Views - Last Post: 16 February 2010 - 08:31 AM Rate Topic: -----

#1 sns.cool   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-November 09

random filename generation

Posted 15 February 2010 - 12:27 PM

i am creating a web browser in which i want to create history feature and i want to create the files in the hard drive as by the name of that day date.please help
Is This A Good Question/Topic? 0
  • +

Replies To: random filename generation

#2 SixOfEleven   User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

Re: random filename generation

Posted 15 February 2010 - 12:34 PM

DateTime.Now



Will give you the current date and time. You should be able to use that in the generation of your filenames.

For example, you could create a string as follows to create a string that has the day, month, year, hour and minute.

            string dateString = DateTime.Now.Day.ToString()
                + DateTime.Now.Month.ToString()
                + DateTime.Now.Year.ToString()
                + DateTime.Now.Hour.ToString()
                + DateTime.Now.Minute.ToString();


Was This Post Helpful? 0
  • +
  • -

#3 sns.cool   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-November 09

Re: random filename generation

Posted 15 February 2010 - 01:56 PM

View PostSixOfEleven, on 15 February 2010 - 11:34 AM, said:

DateTime.Now



Will give you the current date and time. You should be able to use that in the generation of your filenames.

For example, you could create a string as follows to create a string that has the day, month, year, hour and minute.

            string dateString = DateTime.Now.Day.ToString()
                + DateTime.Now.Month.ToString()
                + DateTime.Now.Year.ToString()
                + DateTime.Now.Hour.ToString()
                + DateTime.Now.Minute.ToString();


for Ex this is a code now i want to generate the filenames at run time that is what i am trying to ask
in this as i put the file name it takes the filename as date but i want it to be the actual date and time or any random number.so that more number of file can be created at run time as per required.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace ConsoleApplication1
{
    class Program
    {
        static void write()
        {
            string date;
            date = DateTime.Now.Day.ToString();
            FileStream asd = new FileStream(@"c:\date.str", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            StreamWriter ase = new StreamWriter(asd);
            ase.WriteLine(DateTime.Today.Month);
            ase.Close();
            asd.Close();    
        
        }
        static void read()
        {
            FileStream asd = new FileStream(@"c:\date.str", FileMode.Open, FileAccess.ReadWrite);
            StreamReader ase = new StreamReader(asd);
            Console.WriteLine(ase.ReadLine());
            ase.Close();
            asd.Close();    
             
        
        
        }
        static void Main(string[] args)
        {
            write();
            read();
            Console.Read();

        }
    }
}



Mod Edit:
When posting code please use code tags like below. Thanks.
:code:
Was This Post Helpful? 0
  • +
  • -

#4 SixOfEleven   User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

Re: random filename generation

Posted 15 February 2010 - 04:14 PM

You had the right idea. Instead of putting the variable name inside a string, use it for the filename. You can try something like the following.

            string date;
            date = DateTime.Now.Day.ToString();
            string filename = @"C:\path\history" + date + ".str";
            FileStream asd = new FileStream(filename, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            StreamWriter ase = new StreamWriter(asd);
            ase.WriteLine(DateTime.Today.Month);
            ase.Close();



This will create a file like: C:\path\history15.str

You should probably use a different directory that C:\ though. The StartupPath would probably be a better idea.
Was This Post Helpful? 0
  • +
  • -

#5 Adkins   User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: random filename generation

Posted 16 February 2010 - 02:58 AM

I would also recommend using more than just the day. Every (approx) 30 days you will end up with the same file name. Unless of course you are going to use that very point as a way to minimize the number of files being created. Another idea would be to have the number of days history is saved be left up to the user. The user could input the number of days and you could count from 1 to that number and then start back over at 1. That way however many days the user wants to save will be. Of course this would require a viewer to help the user decipher which number is which day in the list. Unless the current day is always day one and tomorrow you make that day 2 and the new one day 1. I have now strayed WAYYYYY beyond your question. Sorry about that. Hope you enjoyed the read.
Was This Post Helpful? 0
  • +
  • -

#6 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: random filename generation

Posted 16 February 2010 - 05:41 AM

View PostSixOfEleven, on 15 February 2010 - 02:34 PM, said:

DateTime.Now



Will give you the current date and time. You should be able to use that in the generation of your filenames.

For example, you could create a string as follows to create a string that has the day, month, year, hour and minute.

            string dateString = DateTime.Now.Day.ToString()
                + DateTime.Now.Month.ToString()
                + DateTime.Now.Year.ToString()
                + DateTime.Now.Hour.ToString()
                + DateTime.Now.Minute.ToString();



Wouldn't this be easier?

// will give you 20100216074155
string dateString = DateTime.Now.ToString("yyyyMMddHHmmss");



If knowing what "MM" and "dd" is the problem, it takes a simple Google search to find that out.
Was This Post Helpful? 0
  • +
  • -

#7 sns.cool   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 18-November 09

Re: random filename generation

Posted 16 February 2010 - 07:33 AM

how to make the file generation dynamic or in particular i want to name the files according to the date of that day as i am making web browser and i want to make a history file seprate for each day
here is the bit of code

WebBrowser webtitle = GetCurrentWebBrowser();
                this.Text = webtitle.DocumentTitle + "-iBrowser";
            //date
            
            date=Convert.ToString(DateTime.Today.Date);
            FileStream his=new FileStream("c:\\history.txt",FileMode.OpenOrCreate,FileAccess.ReadWrite);
            StreamReader asn = new StreamReader(his);
            asn.BaseStream.Seek(5,SeekOrigin.Begin);
            date1=Convert.ToString(asn.ReadLine());
            
            if(date==date1)
            {
            asn.BaseStream.Seek(0,SeekOrigin.End);
            asn.Close();
            his.Close();
            FileStream fd = new FileStream("c:\\history.txt", FileMode.Append, FileAccess.Write);
            StreamWriter hisw=new StreamWriter(fd);
            date=Convert.ToString(DateTime.Today.Date);
            hisw.WriteLine(DateTime.Now.Date);
            //hisw.WriteLine(DateTime.Today.Date);
            hisw.WriteLine();
            hisw.WriteLine();
            //hisw.Write(lno);
            hisw.Write(webtitle.DocumentTitle);
            hisw.WriteLine();
            hisw.Close();
            fd.Close();
            }
            else
            {
            FileStream ak=new FileStream("c:\\history.txt",FileMode.Open,FileAccess.ReadWrite);
            StreamWriter asde= new StreamWriter(ak);
            asde.WriteLine(date);asde.WriteLine();asde.WriteLine();
                asde.WriteLine(DateTime.Today.Date);
                asde.WriteLine(webtitle.DocumentTitle);
                asde.Close();
                ak.Close();             
            }
        }

This post has been edited by JackOfAllTrades: 16 February 2010 - 08:30 AM
Reason for edit:: Added code tags. Please [code]...place your code in here...[/code]

Was This Post Helpful? 0
  • +
  • -

#8 Adkins   User is offline

  • D.I.C Addict
  • member icon

Reputation: 66
  • View blog
  • Posts: 560
  • Joined: 27-October 09

Re: random filename generation

Posted 16 February 2010 - 07:34 AM

please surround code with :code: to make it more readable. Also please don't open a new topic to re-ask the same question. That is just rude.
Was This Post Helpful? 0
  • +
  • -

#9 n8wxs   User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: random filename generation

Posted 16 February 2010 - 07:36 AM

Please edit your post to add the code tags around your code:

:code:

Thanks!

This post has been edited by n8wxs: 16 February 2010 - 07:37 AM

Was This Post Helpful? 0
  • +
  • -

#10 eclipsed4utoo   User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1536
  • View blog
  • Posts: 5,972
  • Joined: 21-March 08

Re: random filename generation

Posted 16 February 2010 - 08:00 AM

why did you create another thread when you have this one already?
http://www.dreaminco...topic156174.htm
Was This Post Helpful? 0
  • +
  • -

#11 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6259
  • View blog
  • Posts: 24,028
  • Joined: 23-August 08

Re: random filename generation

Posted 16 February 2010 - 08:31 AM

Merged topics. Please don't open new topics when you have an active one regarding the same issue.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1