0

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.

1
  • Yes, I know code will break on many machines without the try-catch block, but this was just a test program to extract files. My question is why it deletes content of one folder and extracts in the correct folder. Oct 21, 2012 at 13:03

2 Answers 2

3

\temp will use the root of current directory (G: in this case).

You should use Application.StartupPath() to get the path to your application. Then Path.Combine(startupPath, tempPath) which you can use to create and load with DirectoryInfo.

Also consider using the actual temp-folder of your computer environment:

string tmpFolder = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);

Sorry, it should be:

tmpFolder = System.IO.Path.GetTempPath;

Also, to delete all files in the folder you can use this method:

IO.Directory.Delete(tmpFolder, true);
3
  • Yes, I checked for that extra slash, but the thing is then why my working folder was still program's temp directory. I used Environment.CurrentDirectory to save initially, but changed to this form. Oct 21, 2012 at 12:28
  • The \ will force going to the root. CD is not necessarily the same as program folder. if you go via f.ex. Explorer to the program folder and start the exe from there, CD will initially be the same as program folder. However, if you start form a link (ie. shortcut) or a relative path, CD is most likely not the same as program folder. The Application.StartupPath will guarantee you that you get program startup path (using Env..SetCD will require that you know program startup path). But for temp files, the environment temp is a better choice IMO.
    – user1693593
    Oct 21, 2012 at 12:45
  • Ok, now that's something I didn't know. As for the temp path, actually this is just a test program. my original question is why it shows different values. Oct 21, 2012 at 12:59
1

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.

1
  • "you cannot arbitrary create directories in the root of the drive", didn't know that, thanks! Oct 21, 2012 at 13:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.