Click here to Skip to main content
15,884,648 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
HI I want to encrypt a file by relative file. I use this code to encryption.
private void EncryptFile(string inputFile, string outputFile)
{
    try
    {
        string password = @"Cortex98";
        UnicodeEncoding UE = new UnicodeEncoding();
        byte[] key = UE.GetBytes(password);

        string cryptFile = outputFile;
        FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);

        RijndaelManaged RMCrypto = new RijndaelManaged();

        CryptoStream cs = new CryptoStream(fsCrypt,
            RMCrypto.CreateEncryptor(key, key),
            CryptoStreamMode.Write);

        FileStream fsIn = new FileStream(inputFile, FileMode.Open);

        int data;
        while ((data = fsIn.ReadByte()) != -1)
            cs.WriteByte((byte)data);


        fsIn.Close();
        cs.Close();
        fsCrypt.Close();
    }
    catch
    {
        MessageBox.Show("Encryption failed!", "Error");
    }
}


When I use an absolute path like:
EncryptFile(@"C:\mov\input.mp4",@"C:\mov\output.mp4");


Its works successfully. But when I use:

string inputRelative = @".\mov\input.mp4";
string outputRelative= @".\mov\output.mp4";
string input = Path.GetFullPath(inputRelative);
string output = Path.GetFullPath(outputRelative);
EncryptFile(input,output);


This returns Encryption failed! Error


An unhandled exception of type 'System.IO.DirectoryNotFoundException' occurred in mscorlib.dll Additional information: Could not find a part of the path 'C:\Users\Qsk\Desktop\FINFORM\cortexForm\bin\Debug\lib\corte‌​x\vCom.cortex


What I have tried:

<pre>string inputRelative = @".\mov\input.mp4";
string outputRelative= @".\mov\output.mp4";
string input = Path.GetFullPath(inputRelative);
string output = Path.GetFullPath(outputRelative);
EncryptFile(input,output);
Posted
Updated 22-Aug-17 8:40am

1 solution

Look at the error message:
An unhandled exception of type 'System.IO.DirectoryNotFoundException' occurred in mscorlib.dll Additional information: Could not find a part of the path 'C:\Users\Qsk\Desktop\FINFORM\cortexForm\bin\Debug\lib\corte‌​x\vCom.cortex

It couldn't be a lot clearer!
It is saying that part of the path does not exist: a relative path is relative to the current folder, which is normally the folder the executable you are running it located in.
In order to save (or open) the file, the entire path must be created first, and it must have the correct permissions for the user the application is running under. Check the path, and use the Directory.CreateDirectory method to make it if necessary.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900