System.IO.DirectoryNotFoundException' occurred in mscorlib.dll..please help

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine

I am writing a file compression console application via c#. and witin a compression class in my project Its throwing an exception in the following line of code:

FileInfo[] files = di.GetFiles();

I have hard coded the source and target paths for compression within the program for testing purposes. I have a partitioned Drive -> G:Visual StudioDevMyproject.....

Any suggestions on why i am getting this exception...?

Here is the full program code:

using System.Threading;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Configuration;
using System.IO.Packaging;
using System.Net.Mime;
namespace myNameSpace
{
class Compress
{
public string sourcePath;
public string destPath;


public Compress()
{
this.sourcePath = "";
this.destPath = "";
}

public void ZipFiles(string sourcePath, string destPath)
{

try
{
DirectoryInfo di = new DirectoryInfo(sourcePath);

FileInfo[] files = di.GetFiles();
FileAttributes fileAttributes;
Package zip = ZipPackage.Open(destPath, FileMode.OpenOrCreate, FileAccess.ReadWrite);



//exctract files via for loop
for (int i = 0; i < files.Length; i++)
{
fileAttributes = File.GetAttributes(files.FullName);
bool isArchive = ((File.GetAttributes(files.FullName) & FileAttributes.Archive) == FileAttributes.Archive);
if (isArchive)//if it is to be archived then archive it
{
addToArchive(zip, files.FullName);
// add each member in the directroy to an archive
}
File.SetAttributes(files.FullName, FileAttributes.Normal);
}

}
catch(UnauthorizedAccessException e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
Console.WriteLine("THE SOURCE OF THIS BULLSHIT IS: " + e.Source);
Console.WriteLine("TRY THIS CRAP : " + e.HelpLink);
return;
}



}


public void addToArchive(Package zip_file, string fileToAdd)
{
//replace spaces in path
string zip_uri = Path.GetFileName(fileToAdd);
Uri part_uri = new Uri(zip_uri, UriKind.Relative);
string content_type = MediaTypeNames.Application.Zip;

PackagePart pkg_part = zip_file.CreatePart(part_uri, content_type, CompressionOption.Normal);
Byte[] bites = File.ReadAllBytes(fileToAdd);
pkg_part.GetStream().Write(bites, 0, bites.Length);

}
}



class Program
{
static void Main(string[] args)
{

Compress zip = new Compress();


//obtain names of alll logical drives on the C drive

Console.WriteLine("Total Free Space :" + di.TotalFreeSpace);
Console.WriteLine("Volume Label" + di.VolumeLabel);

Console.WriteLine("Directory Attributes : " + dir.Attributes.ToString());

System.Environment.GetLogicalDrives();
FileInfo[] fileNames = dir.GetFiles("*.*");

foreach (FileInfo fi in fileNames)
{
Console.WriteLine("FILE NAMES : {0} LAST ACCESS TIMES : {1} LENGTH : {2}", fi.Name, fi.LastAccessTime, fi.Length );
}
DirectoryInfo[] dirInfo = dir.GetDirectories("*.*");
Console.WriteLine("nSubdirectories of the root Folder :n");
foreach(DirectoryInfo d in dirInfo)
{
Console.Write("* -> " + d.Name + "n");
}

string currentDirName = Directory.GetCurrentDirectory();
Console.WriteLine("nThe Current Directory is :" + currentDirName);


//get an array of file names as strings rather than fileInfo Objects.


//file name reference for a while before you try to access the file
string[] files = Directory.GetFiles(currentDirName, "*.txt");

foreach(string s in files)
{

//create the fileinfo object only when needed to ensure the information given


// is as current as possible
FileInfo fi = null;
try
{
fi = new FileInfo(s);
}
catch(FileNotFoundException e)
{

//inform the user and continue
Console.Write(e.Message);

continue;
}
Console.WriteLine("File Name :{0} : Directory:{1}", fi.Name, fi.Directory);
}


//change the directory . in this case first check that the file is the first place

// if it doesnt do create a new one
if (!Directory.Exists(@"C:UsersDocumentsTestFolder"))
{
Directory.CreateDirectory(@"C:UsersDocumentsTestFolder");
}

Directory.SetCurrentDirectory(@"C:UsersDocumentsTestFolder");

currentDirName = Directory.GetCurrentDirectory();
Console.WriteLine(currentDirName);


// keep console window open

string sourceFile = "";
string destFile = "";
string file_Dir = "";
string name_Dir = "";

Console.WriteLine("Enter the source file from which you wish to zip : ");
sourceFile = "G:\MMF\NetMMFCopyFile";
Console.WriteLine("Enter the Destination file to which you wish to zip : ");
destFile = "G:\dev";



name_Dir = Path.GetFileName(sourceFile);
file_Dir = string.Format("{0} (Back Up).zip", name_Dir);
file_Dir = destFile + "\" + file_Dir;



// <--------------ERRORS OCCUR FOR PERMISSIONS

Console.WriteLine("nThis is the file_Dir output : " + file_Dir);
Console.WriteLine("This is the Name_Dir output : " + name_Dir);
Console.WriteLine("This is the sourceFile output : " + sourceFile);
Console.WriteLine("This is the destFile output : " + destFile + "n");


Console.WriteLine("About to enter zipping............................n");
zip.ZipFiles(sourceFile, destFile);
Console.WriteLine("Press any key to exit");
Console.ReadKey();


}
}
}








thanks in advance

Saray


View the full article
 
Back
Top