I'm trying to extract some files from a Zip file, but the FastZip.ExtractZip method I was using is having some issues, for example:
Output location: C:\testing\output\
File 1: PhysicalMemory/idx - this is a file, but is created as a directory
File 2: c:/pagefile.sys/00000052 - This is a directory, but is created as a file
File 3: c:/pagefile.sys/00000052/index - This is a file, but is created as a directory
I'm not sure how to correctly identify these as files or directories, as some of the files don't have file extensions, which the FastZip package seems to use to identify files.
The ZipEntry class has a isDirectory method, but it's returning false for every entry, so I can't use that.
Does anyone have any suggestions on how to approach this?
Write your own is directory method to establish file or directory
public bool isDirectory(string path)
{
FileAttributes attr = File.GetAttributes(path);
if (attr.HasFlag(FileAttributes.Directory))
return true;
else
return false;
}
Related
In my solution, I have static file directory at project ApiCon and my code is need to get static file directory is in project BLL and the file path url is /image/myimg.png.
Now that file can show in front end completely. I need to delete my image in static file directory when I call Api in controller, But I can't get path to delete that file.
How to get static file directory?
if (File.Exists(path))
{
File.Delete(path);
}
P.S. I need not to use Directory.GetParent() for find directory in other project.
Static files, while they should be located in the application folder, can be placed in whatever folder you want.
They could be in:
application_root/files/
application_root/files/static/
application_root/superman/batman/robinhood/
On the plus side, you get to put them wherever you want, even in multiple location. On the down side, there is no default static file directory.
Which means you'll have to keep track of the directory itself. The best way to store such information is in the web.config file of your application.
1. Add the config key in the <appSettings> of your web.config file.
<add key="StaticFileDirectory" value="MyStaticFiles" />
2. Add a reference to System.Configuration in your project.
It's listed under Assemblies > Framework.
3. Retrieve the config value.
string foldername = ConfigurationManager.AppSettings["StaticFileDirectory"];
4. Convert it to an absolute path.
string absoluteFolderPath = System.IO.Path.Combine(HttpRuntime.AppDomainAppPath, foldername);
5. Use it as you want to.
string absoluteFilePath = System.IO.Path.Combine(absoluteFolderPath, filename);
if (File.Exists(absoluteFilePath))
{
File.Delete(absoluteFilePath);
}
You can solve this issue by this way.
This function will search all subdirectories inside the given folder path ex:"C:\Users\NBY81HC\Desktop\hello" to return the file you want.
foreach (string file_path in Directory.GetFiles(#"C:\Users\NBY81HC\Desktop\hello", "test.txt", SearchOption.AllDirectories))
{
Console.WriteLine(file_path);//Return C:\Users\NBY81HC\Desktop\hello\aa\a2\test.txt
File.Delete(file_path);
}
one .txt file is getting exported inside the path - D:\work\int\retail\store\export after i run a Stored procedure. Now i want to validate in C# whether the .txt file has come or not in this path. I am using the below syntax according to which file.exists() is still returning false even though .txt file is there in the export location.what's going wrong here?Any help will be appreciated on this.how can i get the latest file name coming in this location dynamically and pass to this below query?
var FilePath = #"D:\work\int\retail\store\export";
if(File.Exists(FilePath))
{
//do this
}
for checking if specific files exists on a path use File.Exists(path), which will return a boolean indicating whether the file at path exists. In your case
if(File.Exists(#"D:\work\int\retail\store\export\one.txt"))
{
//do this
}
In your example you are missing the filename.
If you want to fetch the latest file from some directory use this code.
var directory = new DirectoryInfo(#"D:\work\int\retail\store\export");
var File = directory.GetFiles()
.OrderByDescending(f => f.LastWriteTime)
.First();
You have to create a variavble of DirectoryInfo Class which takes directory path as parameter, Here I have passed your directory path D:\work\int\retail\store\export, Now GetFiles() function returns all the files inside the directory and I have sorted them in Descending order by LastWriteTime property of files, and fetched the first file which will be the latest file in the directory. Hope it helps.
To get the .txt file only Please use below code. It will get you the latest txt file.
var directory = new DirectoryInfo(#"C:\Users\Saket\Downloads\");
var File = directory.GetFiles().Where(c=>c.Extension == ".txt")
.OrderByDescending(f => f.LastWriteTime)
.First();
You need to mention your text file name in the path, if your txt file is called x.txt for example, you need to write the path as var FilePath = #"D:\work\int\retail\store\export\x.txt";
I have some temporary files in Temporary Internet file folder i need copy them to my folder,i see the file in folder but function File.Exists no.
My function
string InternetTempPath= Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);
string TempFilePath = Path.Combine(InternetTempPath, "MyFile.pdf");
bool Isfile = System.IO.File.Exists(TempFilePath);
don't see the files that i am looking for.
Files in Temporary Internet file folder do not have names they can't even be renamed,I think i need to look files by Internet Adress,or Last Checked.They not like ordinary files.
How can i find this files?
Are you missing the file extension?
Your file is probably "MyfileName.txt" not just "MyfileName".
Try adding the file extension and seeing if that works...
string TempFilePath= Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);
TempFilePath+="MyfileName.txt";
bool Isfile = System.IO.File.Exists(TempFilePath);
P.S. appending strings is not recommended in C# the way you're using +=. If these were normal strings I'd recommend using a StringBuilder to combine them, as you're dealing with Paths, try using Path.Combine:
string TempFilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.InternetCache), "MyfileName.txt");
bool Isfile = System.IO.File.Exists(TempFilePath);
Solution 1:
You need to add backward slash after the Temparary internet folder path.
TempFilePath += "\\myfile.txt";
Solution 2: (Recommended)
You can use Path.Combine() to combine the paths as below:
string newpath = Path.Combine(TempFilePath,"myfile.txt");
The problem is most likely the miss of slashes. You should use Path.Combine instead of concatenating the file path yourself:
string TempFilePath = Environment.GetFolderPath(Environment.SpecialFolder.InternetCache);
string filePath = Path.Combine(TempFilePath, "MyfileName");
bool Isfile = System.IO.File.Exists(filePath);
There's a hidden folder inside that location called Content.IE5, and that will contain several randomly named folders with the actual temporary internet files inside them.
var path = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.InternetCache),
"Content.IE5");
Here is answer
I have the following code set up to create a zip file of a set of doucments:
public bool CreateDocumentationZipFile(int documentIdentifier, string zipDestinationPath, IList<string> documentPaths)
{
bool zipped = false;
if (documentPaths.Count > 0)
{
using (ZipFile loanZip = new ZipFile())
{
loanZip.AddFiles(documentPaths, false, zipDestinationPath);
loanZip.Save(string.Format("{0}{1}.zip",zipDestinationPath, documentIdentifier.ToString()));
zipped = true;
}
}
return zipped;
}
The issue I have is that when the zip file is created, the folder structure is maintaned within the zip file:
e.g
I am creating a zip of a selection of documents located at
C:\SoftwareDevelopment\Branches\ScannedDocuments\
When the created zip file is opened, there is a folder structure within the zip as follows:
Folder 1 ("SoftwareDevelopment")
Inside Folder 1 is folder 2 ("Branches")
Inside Folder 2 is folder 3 ("ScannedDocuments")
the scanned documents folder then contains the actual scan files.
Can anyone tell me how I can just have the scan files in the zip without the folders path being maintained?
The documentation states that the third parameter
directoryPathInArchive (String)
Specifies a directory path to use to override any path in the file
name. This path may, or may not, correspond to a real directory in the
current filesystem. If the files within the zip are later extracted,
this is the path used for the extracted file. Passing null (Nothing in
VB) will use the path on each of the fileNames, if any. Passing the
empty string ("") will insert the item at the root path within the
archive.
So if you always want to have the files added to the root of your zip archive, change
loanZip.AddFiles(documentPaths, false, zipDestinationPath);
to
loanZip.AddFiles(documentPaths, false, "");
Basically I want a function where I can provide a string for source that can either be a directory name or a file name and then a target that can also be either a directory name or a file name. It would need to fail if you attempt to copy a directory to a specific file name, but work in all other cases. I would also want to be able to pass additional parameters to tell it to overwrite files and/or nuke the target directory prior to copying as well as create the directory if it does not exist. Anything like that already out there?
Here's a basic test I got from a google search...(plus a few modifications)
// get the file attributes for file or directory
string someStringFromUserInput = Console.ReadLine();
FileAttributes attr = File.GetAttributes(someStringFromUserInput);
//detect whether its a directory or file
if((attr & FileAttributes.Directory) == FileAttributes.Directory)
{
Console.WriteLine("It's a directory");
//Do some transfer method
}
else
{
Console.WriteLine("It's a file");
//Do some transfer method
}
EDIT: This is a very basic example, you'd also want to handle FileNotFoundExceptions for when they try to hit a folder that doesn't exist. But obviously you need to add in your logic to do the copy anyway.