المساعد الشخصي الرقمي

مشاهدة النسخة كاملة : How to Add file to the folder only if the file doesnt exist



C# Programming
06-30-2009, 03:10 PM
Hi,

I have a template file in a folder " c:\template_folder".

At runtime, I will create a new folder " c:\new_folder" and wish to copy the template file to the new_folder only if the file doesnt exist.

description:
for the first time, I will copy the template file to the new_folder and rename it with username... so that after first time the loop finishes, i will have 8 excel files with username as the name of the each file.

for the second loop, if I have to copy the template file to new_folder and rename it to the username, if the file with the user name already exists, then it shouldnt copy the file to the folder.

I am addin the snippet of the code for reference.

foreach (FileInfo fi in templateFile)
{
string oldfilename = null;
string newfilename = null;
if (dir.Exists)
{

fi.CopyTo(Path.Combine(dir.ToString(), fi.Name));
FileInfo fileName = new FileInfo(fi.Name);
oldfilename = Path.Combine(dir.ToString(), fileName.ToString());
newfilename = Path.Combine(dir.ToString(), tempUserName + " " + "E" + tempUserID + " VIPv7.0.xls");


//if( !dir.ToString().Contains(newfilename))
foreach( FileInfo fileList in fileNames)
{
if (fileList.Exists == false)
File.Move(oldfilename, newfilename);
}
}
}

please help me in working this.

thanks
ramm