Here is a method from my program
public partial class Form1 : Form
{
DirectoryInfo tempdirectory = new DirectoryInfo(@"\temp");
const string DEFAULT_ADDRESS_TEXT = "<click load button to load a file>";
private void Form1_Load(object sender, EventArgs e)
{
lblAddress.Text = DEFAULT_ADDRESS_TEXT;
if (Directory.Exists(tempdirectory.Name))
{
lblAddress.Text = tempdirectory.FullName;
foreach (DirectoryInfo dir in tempdirectory.GetDirectories())
{
dir.Delete(true);
}
foreach (FileInfo file in tempdirectory.GetFiles())
{
file.Delete();
}
}
Directory.CreateDirectory(tempdirectory.Name);
lblAddress.Text = tempdirectory.Name;
}
}
Program Run Location(PRL)–> G:\Users\\Documents\Visual Studio 2010\Projects\Comics Project\ZipEntryDemo2\ZipEntryDemo2\bin\Debug
In first run of the program a folder named temp gets created at the program location (PRL\temp).
After closing the application and running again the temp directory created at first step is not empty; rather the lblAddress shows the value G:\temp ; which actually doesn’t even exists.
Then I explicitly created a folder G:\temp and put some extra file and folder in it, for test purposes, the program now removes all files and folders from G:\temp and PRL\temp is still as it is with all contents.
Actually my program passes the location of the temp folder, i.e. PRL\temp to a method which extracts it’s contents in that folder. In every run contents always extracts in PRL\temp;
but G:\temp ‘s content is lost provided the folder exists.
One peculiar thing also is that when I was debugging at the first run since the temp directory doesn’t exist’s at PRL if condition statement doesn’t executes and program creates a folder at the correct location, i.e. PRL\temp.
It’s just that files and folders are checked for the wrong folder G:\temp
I am not getting any exception like folder already exists or of any kind till now.
I also noticed that if I change “\temp” to “temp”, now all things happens in the correct location, i.e. PRL\temp; and there is no G:\temp in middle. My program works fine. But I find it weird and would appreciate if someone could explain that. Thanks, and sorry if it’s a very silly question, this is some of my first experiences with IO.
The method which calls unzip methos is
private void btnLoad_Click(object sender, EventArgs e)
{
OpenFileDialog f = new OpenFileDialog();
f.Filter = "Zip Files(*.zip)|*.zip";
f.Multiselect = false;
if (f.ShowDialog() == DialogResult.OK)
{
unzip(f.FileName, tempdirectory.Name, 4096);
}
}
Here tempDirectory.Name is passed to unzip function, it doesn’t get manipulated in that method. 4096 is buffer size, irrelevant.
These are the kind of things that go wrong when you don’t specify a full path name. You got the first “temp” directory when you started out with specifying just plain
temp, the second one when you changed it to\temp. Which is still not a full path name since it doesn’t specify the drive.This code is also very unlikely to survive on a machine other than your dev machine, you cannot arbitrary create directories in the root of the drive. UAC prevents creating directories and files like that. You’d need an installer to create such directories, only it has sufficient rights.
Instead, a Windows program should use an appdata folder, you get the path to the appdata directory with Environment.GetFolderPath(). If these folders and files are truly temporary in nature, in other words you only need them for the life of the process or can do without them when you startup again, then you should use System.IO.Path.GetTempPath() to get the full path name of the user’s temporary directory.