How to delete zip folder programmatically? - c#

I want to delete a zip folder if it is exists. I have below code.
string zippath = #"C:\Neenu\Downloads.zip";
ZipFile.CreateFromDirectory(#"" + TemporaryFolder, #"" + zippath);
Just before the above code I want to check if folder exists or not. If exists I want to delete folder.

I think you meant How to delete zip file and not a folder.
Here, this should be easy:
File.Delete(zippath);
For deleting inner files and directories :
System.IO.DirectoryInfo di = new DirectoryInfo(path);
foreach (FileInfo file in di.GetFiles())
{
file.Delete();
}
foreach (DirectoryInfo dir in di.GetDirectories())
{
dir.Delete(true);
}

Refer below codeI am keeping a copy of txt file and after that creating a new one
If you wish to delete file without vreating backup than jst use File.Delete(path of file)
if (File.Exists(file_path))
{
new_file_path = file_path.Replace(".txt", " created on " + File.GetLastWriteTime(file_path).ToString("dd-MM-yyyy hh-mm-ss tt") + ".txt");
File.Move(file_path, new_file_path);
File.Delete(file_path);
}

Related

How do I add a resource folder to app data?

I have a folder that contains many png images in my resources of my WPF application. I would like to add this folder to appdata/roaming. Since there are many images that are sorted in folders, I would prefer to add the whole folder. Is there any way to do this?
By the way, the folder is in a directory called Resources and all the pngs are build as resources in Visual Studio.
Here is my code example for your question:
public void MyCopy()
{
//1.You can use the following code to get the path appdata/roaming, and suppose you want to copy the files to NewImageFolder
string strTarget = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).ToString() + "\\NewImageFolder\\";
if (!Directory.Exists(strTarget))
{
Directory.CreateDirectory(strTarget);
}
//2. Then you need to get the path of your the File to be copied
string strSource = AppDomain.CurrentDomain.BaseDirectory;
strSource = strSource.Substring(0, strSource.LastIndexOf("bin")) + "Resources\\Image";
//3.Copy the file
DirectoryInfo dir = new DirectoryInfo(strSource);
FileInfo[] files = dir.GetFiles();
string strFileName = null;
foreach (FileInfo file in files)
{
strFileName = strSource + "\\" + file.Name;
File.Copy(strFileName, System.IO.Path.Combine(strTarget, System.IO.Path.GetFileName(strFileName)));
}
}

Visual C#: Move multiple files with the same extensions into another directory

guys. I've got a problem I can't solve:
I have a 2 folders I choose with folderBrowserDialog and tons of files in source directory I need to move to the target directory. But, I have only to move files with a specific extension like .txt or any other extension I can get from textbox.
So how can I do it?
First get all the files with specified extension using Directory.GetFiles() and then iterate through each files in the list and move them to target directory.
//Assume user types .txt into textbox
string fileExtension = "*" + textbox1.Text;
string[] txtFiles = Directory.GetFiles("Source Path", fileExtension);
foreach (var item in txtFiles)
{
File.Move(item, Path.Combine("Destination Directory", Path.GetFileName(item)));
}
Try this:
For copying files...
foreach (string s in files)
{
File.Copy(s, "C:\newFolder\newFilename.txt");
}
for moving files
foreach (string s in files)
{
File.Move(s, "C:\newFolder\newFilename.txt");
}
Example for Moving files to Directory:
string filepath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
DirectoryInfo d = new DirectoryInfo(filepath);
foreach (var file in d.GetFiles("*.txt"))
{
Directory.Move(file.FullName, filepath + "\\TextFiles\\" + file.Name);
}
will Move all the files from Desktop to Directory "TextFiles".

Copy folder data to other folder in network

De
I have to copy folders and files from one network folder to other. There are some files which cannot be copied since it is named with special characters.
There are so many folders and sub-folders with 3 GB of data. To avoid this problem, I want to write a C# program which can copy all folders, sub-folders and files
with a log file(notepad). Log file which should note the non-copying file details and its path so that easy to trace them further. Can anybody please help me quickly
by providing a c# program or at-least a reference. A console or Win-form application, I am using Visual studio 2010 and Windows 7
copying like below
Copy form :- https://ap.sharepoint.a5-group.com/cm/Shared Documents/IRD/EA
To :- https://cr.sp.a5-group.com/sites/cm/Shared Documents/IRD/EA
here, try this:
Edit: my answer works for local Network Directories, I didn't mention, that you want to copy directiories from HTTPS, for that you have to use WebClient with Credentials
class DirectoryCopyExample
{
string pathFrom = "C:\\someFolder";
string pathTo = "D:\\otherFolder";
string LogText = string.Empty;
static void Main()
{
// Copy from the current directory, include subdirectories.
DirectoryCopy(pathFrom, pathTo, true);
}
private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
// Get the subdirectories for the specified directory.
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
DirectoryInfo[] dirs = dir.GetDirectories();
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ sourceDirName);
}
// If the destination directory doesn't exist, create it.
if (!Directory.Exists(destDirName))
{
Directory.CreateDirectory(destDirName);
}
// Get the files in the directory and copy them to the new location.
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
try
{
string temppath = Path.Combine(destDirName, file.Name);
file.CopyTo(temppath, false);
}
catch(Exception)
{
//Write Files to Log whicht couldn't be copy
LogText += DateTime.Now.ToString() + ": " + file.FullName;
}
}
// If copying subdirectories, copy them and their contents to new location.
if (copySubDirs)
{
foreach (DirectoryInfo subdir in dirs)
{
string temppath = Path.Combine(destDirName, subdir.Name);
DirectoryCopy(subdir.FullName, temppath, copySubDirs);
}
}
}
}
at the end you have to save the variable LogTex to the file, or whatever you need
source: http://msdn.microsoft.com/en-us/library/bb762914(v=vs.110).aspx

delete files which are more than one month old using c#

how to delete files which are more than one month old using c# script.
i am using framework 2.0..
string path = #"C:\Temp\"; //"
DirectoryInfo dirInfo = new DirectoryInfo(path);
FileInfo[] fileInfos = dirInfo.GetFiles();
foreach (FileInfo fileInfo in fileInfos)
{
if (fileInfo.LastWriteTime < DateTime.Now.AddMonths(-1))
fileInfo.Delete();
}
You can call Directory.GetFiles to find all files in a folder.
You can call File.GetLastWriteTime to check when the file was modified.
You can call File.Delete to delete a file.

How to Add file to the folder only if the file doesnt exist using C#

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
To conditionally move a file only if it doesn't already exist you would do it like this:
if (!File.Exists(newfilename))
{
File.Move(oldfilename, newfilename);
}
Your code snippet confuses me, so I hope I've answered your question correctly. If I'm missing something please let me know.
Your code doesn't seem correct to me (it doesn't compile), but you can check if a file exists by calling File.Exists(filename), so:
foreach( FileInfo fileList in fileNames)
{
if (!File.Exists(newfilname))
File.Move(oldfilename, newfilename);
}
You want to use File.Exists(path) instead of the commented out line to check if the file exists

Categories