I am very new to coding and I am having some trouble in extracting a zip file to a directory.
Currently this is my code
string zipPath = "Typhoon.zip";
string extractPath = str_xp11_loc + #"\Resources\Plugins";
ZipFile.ExtractToDirectory(zipPath, extractPath);
When using code above I am getting Invalid characters in path
I have tried code that creates a txt file with the extracting location and adding the "#"\Resources\Plugins" via FileWriter and that sorta works but only extracts to the first few folders defined. When I use filewriter it creates a new line with "Resources\Plugins" When using this method it extracts to the first line for example C:\Folder1\Output.
I have tried using string result = Regex.Replace("Xp11_install.txt", #"\r\n?|\n", "");
to try and remove the the line break but that has not worked.
Is it possible to extract file using the method above?
Any insight is greatly appreciated
I'm not quite sure if this is what you are searching for, but in my case I created a ZIP file with a folder and Subfolder inside.
With the following modified code that you provided:
string zipPath = #"YourFilePath.zip";
string extractPath = Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
ZipFile.ExtractToDirectory(zipPath, extractPath);
I was able to extract that file, in this example to the MyPictures folder, with all the Subfolders.
If this is not what you were searching for, maybe have a look at the docs documentation:
ZipFile.ExtractToDirectory Method
Related
I'm trying to extract a zip file using DotNetZip. Whenever I try to extract an existing zip file with a path, I get a DirectoryNotFoundException. The path I am passing in gets changed for some reason, causing the DirectoryNotFoundException. I'm following the examples found here.
Here is my code:
string zipToUnpack = #"C:\Users\user\Desktop\data.zip";
string TmpDirectory = #"C:\Program Files\Temp";
using (ZipFile zip = ZipFile.Read(zipToUnpack))
{
zip.ExtractAll(TmpDirectory, true);
}
The DirectoryNotFoundException shows me the path it is looking in and it isn't TmpDirectory. Instead, it's TmpDirectory + another location in my ProgramFiles directory.
I'm trying to move a file from the desktop to a directory called "Textfiles" but every time I try to it gives me this error.
Additional information: The target file "C:\Users\Developer\Documents\Textfiles" is a directory, not a file.
Now I know that using
File.Copy(fileName, targetPath);
Would be wrong and that's what I am using right now, It takes two parameters, the first being the file yopu want to copy and the second one being the file it's replacing? Correct me if i am wrong on the second parameter.
Anyways, I tried System.IO.Directory.Move(fileName, destFile); aswell but that pretty much gave me the same error.
The two parameters are very simple, just two string that consists of paths.
string fileName = filePath.ToString();
string targetPath = #"C:\Users\Developer\Documents\Textfiles";
What would be the correct way to transfer fileName to targetPath ?
You need to specify the destination filename.
string fileOnly = System.IO.Path.GetFileName(fileName);
string targetPath = System.IO.Path.Combine(#"C:\Users\Developer\Documents\Textfiles", fileOnly);
System.IO.File.Move(fileName, targetPath);
See https://msdn.microsoft.com/en-us/library/c6cfw35a(v=vs.110).aspx
for documentation:
destFileName
Type: System.String
The name of the destination file. This cannot be a directory or an existing file.
You have to add the new file name to the destination directory.
You can get the file name with:
result = Path.GetFileName(fileName);
thus in your case:
string targetPath = #"C:\Users\Developer\Documents\Textfiles\" + Path.GetFileName(fileName);
I'm trying to write a method that extracts a zip file to a directory, finds a file in the extracted contents, reads the text in that file to a string, and returns that string. Here is my attempt
private string _getDataFile(string zipFile)
{
string pathToFolder = #"C:\Path\To\The\File";
foreach (char c in Path.GetInvalidPathChars())
{
pathToFolder = Regex.Replace(pathToFolder, c.ToString(), "");
}
string pathToFile = pathToFolder + #"\model.dat";
ZipFile.ExtractToDirectory(zipFile, pathToFolder);
string dataToReturn = File.ReadAllText(pathToFile);
return dataToReturn;
}
However, despite my foreach loop replacing illegal path characters, the program still throws an illegal characters in path exception at the ZipFile.ExtractToDirectory line no matter what directory I try to use and I have no idea why. Any help would be greatly appreciated.
According to a similar post, it looks like you may have a problem with a file name inside the target zip file; it is not a problem with your specified zip file name or directory. Try extracting the contents of the file manually to see if there are unusual file names.
You can iterate through all the entries and sanitize the filenames before extracting them by using this snippet that I wrote here: ZipFile.ExtractToDirectory "Illegal characters in path"
Question Background:
I need to copy and paste (move) a file from one folder location to another.
Issue:
The File.Copy method of System.IO requires the that both parameters are of known file locations. I only know one file path location - in this case localDevPath. localQAPath is the folder path where I want the copied file to be moved too.
string localDevPath = #"C:\Folder1\testFile.cs";
string localQaPath = #"C:\Folder2\";
File.Copy(localDevPath, localQaPath);
Can anyone tell me how to go about carrying out this 'copy and paste' method I'm trying to implement.
string localDevPath = #"C:\Folder1\testFile.cs";
string localQaPath = #"C:\Folder2\";
FileInfo fi = new FileInfo(localDevPath);
fi.MoveTo(Path.Combine(localQaPath, fi.Name));
Assuming that these are user-provided paths and you can't simply include the filename in the second path, then you need to extract the last path element from localDevPath and then add it to localQaPath. You could probably do that with Path.GetFilename.
I'm guessing the issue here is that the filename is variable, in which case, you could do something like this to extract the filename from the full path of localDevPath:
string localDevPath = #"C:\Folder1\testFile.cs";
string localQaPath = #"C:\Folder2\";
string[] tokens = localDevPath.Split(#"\");
localQaPath += tokens[tokens.Length-1];
File.Copy(localDevPath, localQaPath);
Documentation on File.Copy is on MSDN. There is an overload that accepts a boolean, to allow overwriting if there is a naming conflict.
If what you want to do is move the file from one location to another, the method you are looking for is MoveTo. It is a method of the FileInfo class. There is a very complete example in the MSDN Library here: FileInfo.MoveTo Example
I have got this read file code from microsoft
#"C:\Users\computing\Documents\mikec\assignment2\task_2.txt"
That works fine when im working on it, but when i am to hand in this assignment my lecturer isn't going to have the same directory as me.
So i was wondering if there is a way to read it from just the file the program is held in?.
I was thinking i could add it as a resource but im not sure if that is the correct way for the assignment it is meant to allow in any file.
Thanks
You can skip the path - this will read file from the working directory of the program.
Just #"task_2.txt" will do.
UPDATE: Please note that method won't work in some circumstances. If your lecturer uses some automated runner (script, application whatsoever) to verify your app then #ken2k's solution will be much more robust.
If you want to read a file from the directory the program is in, then use
using System.IO;
...
string myFileName = "file.txt";
string myFilePath = Path.Combine(Application.StartupPath, myFileName);
EDIT:
More generic solution for non-winforms applications:
string myFilePath = Path.Combine(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location), myFileName);
If it is a command line application, you should take the file name as a command line argument instead of using a fixed path. Something along the lines of;
public static void Main(string[] args)
{
if (args == null || args.Length != 1)
{
Console.WriteLine("Parameters are not ok, usage: ...");
return;
}
string filename = args[0];
...
...should let you get the filename from the command.
You could use the GetFolderPath method to get the documents folder of the current user:
Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
and to exemplify:
string myDocuments = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string file = Path.Combine(myDocuments, #"mikec\assignment2\task_2.txt");
// TODO: do something with the file like reading it for example
string contents = File.ReadAllText(file);
Use the relative path.
you can put your file inside the folder where your application resides.
you can use Directory.GetCurrentDirectory().ToString() method to get the current folder of the application in. if you put your files inside a sub folder you can use
Directory.GetCurrentDirectory().ToString() + "\subfolderName\"
File.OpenRead(Directory.GetCurrentDirectory().ToString() + "\fileName.extension")
StreamReader file = new StreamReader(File.OpenRead(Directory.GetCurrentDirectory().ToString() + ""));
string fileTexts = file.ReadToEnd();