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.
Related
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
Am trying to loop through a folder and read all the XML files within it.
Am using the Directory.EnumerateFiles to read from the path.
My .cs file and the XML files folder lies in same path
"C:\User\Documents\Projects\TestTool"
Am using the below code to get read the files.
string path = #"..\TestCases\";
foreach(string file in Directory.EnumerateFiles(path, "*.xml"))
{
}
Using this am getting exception stating
Could not find path "C:\Program Files(x86)\Common Files\Microsoft
Shared\DevServer\TestCases"
This is pointing to the WebDev.WebServer20 path instead of the actual path.
Not sure why it is pointing to a completely different folder
I tried string path = #"\TestCases\"; But when I try like this it is throwing an exceptiopn stating
Could not find path "C:\TestCases"
What is the mistake am making? Please help
Try it:
string path = "..\\TestCases\\";
foreach (FileInfo file in new DirectoryInfo(path).EnumerateFiles("*.xml"))
{
string filePath = file.FullName;
}
Since it is running under WebDev server it will using WebDev.WebServer20.exe's path for all relative paths.
You must use: Server.MapPath("~") to get to your own root.
string path = Server.MapPath("~") + #"\TestCases\";
foreach(string file in Directory.EnumerateFiles(path, "*.xml"))
{
}
Please note if Server.MapPath("~") points to bin, then use it like Directory.GetParent(Server.MapPath("~")) + #"\TestCases\";
Assuming this is your target directory C:\User\Documents\Projects\TestTool\TestCases
One of my executable process produces two files. I want to move one file that is produced to shared drive. I am writing an automatic process to move the file from one location to shared drive. The only issue is file name changes every time the executable is run so I don't have the exact filename with me. I only have the extension, .xls. I have only one .xls file in my directory.
I tried doing this
File.Copy(#"*.xls", #"\\serv44\Application\testing\name\test2\*.xls", true);
It threw an error saying Invalid name. After moving the file to shared drive. I want to delete the .xls file.
File.Delete("*.xls");
any help will be appreciated
You should get file name and then do whatever you want with that file. I.e. if you have only one xls file in the source directory:
var targetDirectory = #"\\serv44\Application\testing\name\test2\";
var sourceFile = Directory.EnumerateFiles(sourceDirectory, "*.xls").FirstOrDefault();
if (sourceFile != null)
{
var sourceFileName = Path.GetFileName(sourceFile);
var targetFileName = Path.Combine(targetDirectory, sourceFileName);
File.Copy(sourceFileName, targetFileName);
File.Delete(sourceFileName);
}
Note: instead of copy and delete you can use single Move operation.
If you want to move several files from your source directory, then instead of taking first one process all found files in a loop:
foreach(var sourceFile in Directory.EnumerateFiles(sourceDirectory, "*.xls"))
{
var sourceFileName = Path.GetFileName(sourceFile);
var targetFileName = Path.Combine(targetDirectory, sourceFileName);
File.Move(sourceFileName, targetFileName);
}
This should give you that file name:
var fileName = Directory.GetFiles(yourDirectory, "*.xls").ToList().FirstOrDefault();
I've found a few related questions but they're not working that well. The image name is a modified GUID like 3c6b4a9b-8e88-4c8e-93da-258acd2c964f_0 but the extension isn't known (.jpg, .gif, ..etc). The GUID will be coming from a gridview so it's not a static string. Below is what I have but I'm having a difficult time getting the path to work correctly.
string fileName = `3c6b4a9b-8e88-4c8e-93da-258acd2c964f`;
DirectoryInfo filePath = new DirectoryInfo(#"/Images");
MessageBox.Show(filePath.ToString());
FileInfo[] fileArray = filePath.GetFiles(fileName + "_0.*");
Keep getting issues with the directory being invalid. Currently the files are stored on my c: drive.
How can I get the relative path without hardcoding it in? I was using DirectoryInfo(Server.MapPath("Images")); which worked temporarily then started giving this error System.ArgumentException: Second path fragment must not be a drive or UNC name. which seems to be from the path having the drive "C:" This doesn't seem to be a permanent solution once the site is launched though.
The actual path is C:\Website\Name\Images\3c6b4a9b-8e88-4c8e-93da-258acd2c964f_0.jpg
Thanks!
You've used filePath as the first parameter to GetFiles, just use the wildcard and invoke the overload of GetFiles with one parameter.
filePath.GetFiles("_0.*");
The problem is that you are getting DirectoryInfo for "C:\Images".
You want to use Server.MapPath to get the physical path to the folder that is in your website (which could be anywhere on any drive).
Using the ~ means to start from the root of the running website.
So this should do the trick:
string fileName = `3c6b4a9b-8e88-4c8e-93da-258acd2c964f`;
DirectoryInfo filePath = new DirectoryInfo(Server.MapPath("~/Images"));
FileInfo[] fileArray = filePath.GetFiles(fileName + "_0.*");
I'm having problem on getting the file path using file upload. When I tested to upload a file on the file upload, I've noticed that my file upload is getting the wrong path. The right path is C:\RightPath\B1.txt but the I check it its getting the wrong path which is 'C:\Program Files\Microsoft Visual Studio 9.0\Common7\IDE\B1.txt'..
here's my code behind...
string OasisPath = Path.GetFullPath(cmdUpload.FileName);
StreamReader OasisFile = new StreamReader(OasisPath);
string B1String = OasisFile.ReadLine();
OasisFile.Close();
I also tried this one..
string OasisPath = Server.MapPath(cmdUpload.FileName);
StreamReader OasisFile = new StreamReader(Server.MapPath(cmdUpload.FileName)); // I get this error Could not find file 'C:\Rightpath\B1.txt'
string B1String = OasisFile.ReadLine();
OasisFile.Close();
Please advice me...
thanks,,
You need to explicitly set the path of the file when you save it. The server doesn't know what path the file was stored in on the client machine. If you don't specify a path it will just save it in the current environment's default path.