Cant Unzip files from web application? - c#

I am trying to unzip a downloaded file with Ionic.Zip Dll.
My code works in a windows application but it wont work in my Web application.
The code runs with no errors but when i check the folder there is still the Ziped downloaded file with no extractions.
Is there any specificity things a web application needs to do this??
private void MyExtract()
{
string zipToUnpack = "C:\\Users\\Shaun\\Desktop\\RescommTestData\\downloadfile.zip";
string unpackDirectory = "ExtractedFiles"; //used to create a file to extract files to
using (ZipFile zip1 = ZipFile.Read(zipToUnpack))
{
// here, we extract every entry, but we could extract conditionally
// based on entry name, size, date, checkbox status, etc.
foreach (ZipEntry e in zip1)
{
e.Extract(unpackDirectory, ExtractExistingFileAction.OverwriteSilently);
// e.Extract(ExtractExistingFileAction.OverwriteSilently);
}
}
try
{
//File.Delete("C:\\Users\\Shaun\\Desktop\\downloadfile.zip");
MessageBox.Show("Unpacked photos and data file into ExtractedFiles folder in bin/Debug");
}
catch (Exception)
{
MessageBox.Show("Could not delete ZIP!");
Environment.Exit(1);
}

Related

Temp files are not deleting

Hi i am actually creating a program to delete all the temp files and folders when an event is triggered. so for that i am using the following code but its not working and throwing an exception
And also for this i am using the code
private void tempfiles_Click(object sender, EventArgs e)
{
if(tempcheck.Checked)
{
string tempPath = System.IO.Path.GetTempPath();
System.IO.DirectoryInfo di = new DirectoryInfo(tempPath);
try
{
foreach (FileInfo file in di.GetFiles())
{
file.Delete();
}
foreach (DirectoryInfo dir in di.GetDirectories())
{
dir.Delete(true);
}
}
catch(Exception env)
{
MessageBox.Show("Please close all the applications and try \n" + env);
}
}
else
{
MessageBox.Show("Please check the checkbox");
}
}
Here i want to delete the folders and files without any exception but in java i use method like fileonclose().
If files are being used by another program, you will not be able to delete them. You should put your try...catch statement inside each for loop. That way you can continue trying to delete files even if one attempt fails.
To make your program more useful, you can keep track of which files were not deleted and create a log or open a window to show the file names to the user.

Delete IE Explorer cache from Windows application

I need to delete all internet explorer cached files from my application, this is my code:
void clearIECache()
{
ClearFolder(
new DirectoryInfo(
Environment.GetFolderPath(Environment.SpecialFolder.InternetCache)));
}
void ClearFolder(DirectoryInfo folder)
{
foreach (FileInfo file in folder.GetFiles())
{
try
{
file.Delete();
}
catch (Exception)
{
}
}
}
It says that I have no authorization to delete files, I tried to delete files in that folder from console and it gives the same error...
Is there another way to clear explorer cache ?

How to check a folder exists in DropBox using DropNet

I'm programming an app that interact with dropbox by use DropNet API. I want to check if the folder is exist or not on dropbox in order to I will create one and upload file on it after that. Everything seen fine but if my folder is exist it throw exception. Like this:
if (isAccessToken)
{
byte[] bytes = File.ReadAllBytes(fileName);
try
{
string dropboxFolder = "/Public/DropboxManagement/Logs" + folder;
// I want to check if the dropboxFolder is exist here
_client.CreateFolder(dropboxFolder);
var upload = _client.UploadFile(dropboxFolder, fileName, bytes);
}
catch (DropNet.Exceptions.DropboxException ex) {
MessageBox.Show(ex.Response.Content);
}
}
I'm not familiar with dropnet, but looking at the source code, it appears you should be able to do this by using the GetMetaData() method off of your _client object. This method returns a MetaData object.
Example:
//gets contents at requested path
var metaData = _client.GetMetaData("/Public/DropboxManagement/Logs");
//without knowing how this API works, Path may be a full path and therefore need to check for "/Public/DropboxManagement/Logs" + folder
if (metaData.Contents.Any(c => c.Is_Dir && c.Path == folder)
{
//folder exists
}

Open PDF using C#-- Windows 8 Store Application

I want to be able to open a PDF using the native Windows Reader Application when a user clicks on a button. So far I am able to use the following code to successfully open files that end with the (.PNG) extension. However, when I let the link to open the (.PDF) file I get the following error.
The system cannot find the file specified. (Exception from HRESULT: 0x80070002)
The file destination is correct.
Here is my code:
private async void btnLoad_Click(object sender, RoutedEventArgs e)
{
// Path to the file in the app package to launch
string imageFile = #"Data\Healthcare-Flyer.pdf";
var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(imageFile);
if (file != null)
{
// Set the option to show the picker
var options = new Windows.System.LauncherOptions();
options.DisplayApplicationPicker = true;
// Launch the retrieved file
bool success = await Windows.System.Launcher.LaunchFileAsync(file, options);
if (success)
{
// File launched
}
else
{
// File launch failed
}
}
else
{
// Could not find file
}
}
}
When you add PDF document in project, you have to change it's build action.
Right click on PDF document.
Click on properties.
Change Build Action from None to Content

Open A PDF file on button click

I want to open a pdf file in winRT app(metro style app) by clicking on a button the file should open in windows8 default reader. I tried this code, where button click method name is DefaultLaunch_click():
async void DefaultLaunch_click()
{
// Path to the file in the app package to launch
string imageFile = #"images\ret.png";
// Get the image file from the package's image directory
var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(imageFile);
if (file != null)
{
// Set the recommended app
var options = new Windows.System.LauncherOptions();
options.PreferredApplicationPackageFamilyName = “Contoso.FileApp_8wknc82po1e”;
options.PreferredApplicationDisplayName = “Contoso File App”;
// Launch the retrieved file pass in the recommended app
// in case the user has no apps installed to handle the file
bool success = await Windows.System.Launcher.LaunchFileAsync(file, options);
if (success)
{
// File launched
}
else
{
// File launch failed
}
}
else
{
// Could not find file
}
}
It worked for .png file but i want for .pdf file i replaced 1.png with M.pdf(after including it in images folder) and set the build content of M.pdf to Embedded Resource , run the program but it showed error that
**The system cannot find the file specified. (Exception from HRESULT: 0x80070002)**
This code works for me after I set PDF file build action to content and copy always to output directory.
private async void Button_Click(object sender, RoutedEventArgs e)
{
string imageFile = #"images\somepdffile.pdf";
// Get the image file from the package's image directory
var file = await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFileAsync(imageFile);
if (file != null)
{
bool success = await Windows.System.Launcher.LaunchFileAsync(file, options);
if (success)
{
// File launched
}
else
{
// File launch failed
}
}
else
{
// Could not find file
}
}

Categories