what does " parent directory accessors" means? - c#

I am trying to extract files from a folder in C# using ZipFile.ExtractToDirectory(zipFile, extractPath); as explained here http://msdn.microsoft.com/en-us/library/hh485723(v=vs.110).aspx in the example section. Its keep throwing an exception "Extracting Zip entry would have resulted in a file outside the specified destination directory." For this exception Microsoft is saying "this might happen if the entry name contains parent directory accessors." I have no idea what does that mean. I have tried changing zipFile, extractPath to completely different places but still no idea what to do.

The problem is likely with the zip file, not with your code. It is possible to create a zip file with relative positions upwards from the directory in which the zip file is being created, for instance:
"C:\Program Files (x86)\GnuWin32\bin\zip.EXE" d:\bar.zip ..\*
In which case the zip file actually contains "..\" in the relative path information.
It sounds as though ZipFile.ExtractToDirectory() is refusing to unzip such zip files, perhaps for security reasons (I.e. a web server unzipping such a file might find itself overwriting files outside the target directory!) In fact, if I try to open such a zip file using ExtractAll in the windows shell, I get this error:
You can at least open such files in 7zip to see if this is happening (and in fact 7zip will extract files from the crazy zip file I created with the command above.)
If, when creating the zip file, you change directory to the parent directory, rather than zipping the parent directory, then all should be well. I.e.:
pushd .. & "C:\Program Files (x86)\GnuWin32\bin\zip.EXE" d:\bar5.zip * & popd
will create a zip file that Windows likes, because zip will be creating a zip file relative to the current directory at the moment it is run.

I believe the parent directory accessors are the "." and ".." characters in the path to specify the current and previous directories respectively. Do you have any of them in your extractPath like ..\Folder?

Related

Update an assets file (C#/UWP)

I've got a csv file stored in my solutions's assets folder. Every now and then a copy of the csv is updated on a shared drive. Is there a way to copy this file and replace the version in the assets folder automatically. (I'd just reference the shared file rather than an internal one but occasionally the network plays up. I guess I could just have it copy to a location locally but I'd rather do it this way if possible)
I've added something to copy the file over on app launch but don't have permissions. I've guessing that assets are read only during runtime? Is there a way around this?
This is the line of code that triggers the error. File.Copy(sharedPath, assetPath, true);
Error: Access to the path 'C:\Users...AppX\Assets\file.csv' is
denied.
Update an assets file (C#/UWP)
During checking the path, and it looks a apps installation folder, unfortunately, the installation folder is read-only, we can't write data into. we suggest you use app' local folder that with full permission.
For your scenario, you could copy the csv file where in the assets folder to app's local folder when app fist launch. when you want to update csv file you just need to replace local folder's csv with shared drive.
For more detail please refer this document.

C# get relative path of resources folder

I want to acess a .txt file, which I stored in the resources folder of my project, there where all the imported pictures are stored as well.
I want to use something like a relative path, since every user would have safed his programm somewehere else on his Pc, but there is always the resources folder at the same place, compared to the programm folder.
I tried to used this: displayText = System.IO.File.ReadAllText("Resources\\startmessages.txt"); but this isn't working.
I get this error message: System.IO.DirectoryNotFoundException:, it lists the unrelative path to the .txt there as well, so I don't get, why it cant read it.
Thanks for your Help.
What #ChetanRanpariya is trying to tell you is, that your programm is built in another folder than your folder Resources is sitting to. So you have explictly tell your file Resources\startmessages.txt to copy itself on build process, so it get copied to said another folder. Assuming that you are using Visual Studio, you have to right click on your file and set Copy To Output Directory to true. Its relative folder path (Resources\) will be taken over. You find your build folder somewhere in your bin folder depending on configuration and framework. :)
Current Path where your executable is
Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).FullName
Path to Solution
If you are using Visual Studio and need to access the folders in the solution directory, you can use .Parent method,
Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.Parent.FullName
Use of Path.Combine
and once you have the location of your Resource folder, use Path.Combine to get the location to read files / content etc
Path.Combine(Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.Parent.FullName, "Resources\\startMessages.txt")

How can I provide relative path to command line arguments in start option in C#

I have one directory folder where we are getting 100 files per day.
My program picks the files from the IN folder and puts them in the Out folder after processing them.
I am getting the issue that when I am giving the exact location in
Project > Properties > DEBUG > START OPTION > COMMAND LINE ARGUMENTS
(i.e "C:\Data\IN\File.txt") then the program executes successfully and finds the file, but when I provide the Location like "C:\Data\IN" it is not picking any file and is throwing the exception
Could not find file 'C:\Data\In'. InnerException is Null.
The IN folder is getting 100 different files daily. How can I solve this problem?
You need to use the Directory.GetFiles() static class to get a list of files to process. The Microsoft Documentation has a useful example.
https://msdn.microsoft.com/en-us/library/07wt70x2(v=vs.110).aspx
"C:\Data\IN" is not a relative path, it is an absolute path to the directory containing the files. You can get the files like this from an absolute directory path:
string[] files = Directory.GetFiles(#"C:\Data\IN", "*.txt");
If you only know the path of the directory relative to the EXE you can get the absolute path like this:
string dir = Path.Combine(Application.StartupPath, #"Data\IN");

How to zip selected files? not all the files in a directory in c#

I want to zip some files in a directory. I have googled a lot but i always saw examples about zipping a directory (all files in directory) by the code below:
string startPath = #subPath;
string zipPath = #"C:\result.zip";
ZipFile.CreateFromDirectory(startPath, zipPath);
I am referencing System.IO.Compression.Filesystem.dll
This code zips all the files in the directory referenced by startPath.
I tried ZipArchive class for compressing but it compress file with directories on its path. For example,
assume i want to zip "a.txt" located at "C:\directories\Graphics\a.txt". The zip file contains directories "directories" and "Graphics" and into these directories the file "a.txt". It is not a good solution to cut files then erase the directories and paste again since i am working on big size data.
How can I zip only some of files at a directory?
How can I zip some files that are located in different directories?
I am looking up the subject for 2 days. Is it a kind of constraint in .Net?
You can use the ZipArchive class, first create it and then - once created you just add ZipArchiveEntry objects into the archive. Each of the ZipArchiveEntry objects represents a single file that you will add to the archive.
Some tips:
You can find more info and a sample of how this could be done on MSDN, here: http://msdn.microsoft.com/pl-pl/library/system.io.compression.ziparchive(v=vs.110).aspx and here: http://msdn.microsoft.com/pl-pl/library/system.io.compression.ziparchiveentry(v=vs.110).aspx.
This may not be an exact solution but should work for your scenario. I'll give you the steps :
Create a Temporary Directory
Read and copy your Selected files into the Temporary directory. This applies to your second scenario as well.
Zip the temporary directory
Delete Temporary Directory after you are done
I would recommend using File.Copy to copy all your desired files to a temporary directory, then zip them all from there and delete the temporary directory once the zip operation has finished.

Deploying c# application issues

I have a application with one folder which i added by right clicking the project, selecting add folder. Inside this folder i have xml files which are set to build action:content, copy to output directory: copy if newer (i have tried setting to embedded resource) As well as this i have a few text files and so on.
In my bin/debug output directory i have the exe, the folder with the xml, the stand alone .txt files and so on. My problem is, if i send the exe to my friend to try he always gets an exception thrown.
Say he puts the exe on the desktop, my programme at some point reads the filenames of the xml files in the folder. It uses the following code to do so
String[] filePaths = Directory.GetFiles(#"DataSources\");
I assume that because of this, when the exe runs from the desktop, it expect the folder of .xml files to be in the same place? I have the same type of exception when trying to read the .txt files too. What am i doing wrong here?
Thanks for your time
When reading from files using relative paths you get the one relative to the applications current directory. tip: In C# you can see what directory that is using Environment.CurrentDirectory.
So if you create a shortcut on your desktop, you need to make sure you right click the shortcut and set its "Start in"-folder to the directory of your application. That way its current directory will be set when its started and relative paths will be relative to that path and not the path of the shortcut.
If you actually moved the exe file to the desktop you also need to move any resources that it needs, so if it wants a folder named "datasources" you would have to move that folder as well, or set the current directory when you start the application.
Have you tried something like: http://msdn.microsoft.com/en-us/library/system.windows.forms.application.executablepath.aspx or http://msdn.microsoft.com/en-us/library/system.environment.currentdirectory.aspx ?
So
Directory.GetFiles(environment.currentdirectory + #"\DataSources\");

Categories