Click to See Complete Forum and Search --> : Automatic Naming


blankdev
July 18th, 2006, 09:15 PM
Hello guys, I'm creating a screenshot utility program which obviously takes screenshots. Anyways, I wanted to integrate a feature where one would specify a prefix, and then a number would be appended. For example, if my prefix were screen, and it was the third screen shot I took, it'd save the file as screen03, or something similar, I believe you get the point.

Anyways, I already know how to save and what not, I was just wondering if you guys had an idea as to how I'd accomplish this. I was thinking about doing some type of FileExists method (Is there one already?), and then I'd check if the first shot existed, if it did, then iterate through until the last one didn't exist, then I'd save it as that. I don't know if you guys understand my approach here, if you don't it's okay, but if you do, I'd be greatful to hear your comments.

Here's a rough, 'pseudo-code' visualization:


string file = "pic";
int num = 1;

while (true)
{
if (FileExists((file + num.ToString()) == true)
{
savefileas(file + num.ToString());
break;
}
}

ideru
July 18th, 2006, 09:29 PM
that was how i did it to.

another way i think is to retrieve the list of filenames inside the directory and search thru it.
But i find the above method easier :)

blankdev
July 18th, 2006, 11:52 PM
Yeah, there was actually an error in the code I posted, even if it was pseudo-code. I meant to savefileas(file + (num + 1).ToString());.

Anyways, thanks, I'll just have to look for some type of FileExists method, thanks again ideru.

ideru
July 19th, 2006, 01:38 AM
actually to know if the file exist use the FileInfo class


string filename ="your_filename";
FileInfo fInfo = new FileInfo(filename);

if( fInfo.Exist)
{
// do something
}



Exist is a property of FileInfo in boolean format

blankdev
July 19th, 2006, 10:42 AM
Ah, but then I'd have to create one/define one each time I iterated through the filenames, someone told me to do System.IO.File.Exists(string path), but thanks for the help :)