Search code examples
c#sha1

C# Code doesn’t compile verifying files before process start


I am new to C# trying to get what I hoped might be a simple little script to check the working directory for the existence of test.mdb and then if it exists check the sha1 of MyVbs against MySha and if they’re the same go on to process.Start. Below is my cs file which I write in notepad and compile with framework 3.5 csc.exe

I have reposted my problem having tried all the various and many SO sha1 codes none of which I can get to work with my code and all of which seem to be quite different and specific to certain needs. I settled for the one I thought most appropriate to my situation and incorporated it in to my code but I can’t get it to work? So if anyone can help sort out the problem with my code so that it does work rather than just banning my question it would be very helpful for my learning C#. Thank you.

using System;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;
using System.Security.Cryptography;

    [assembly:AssemblyVersionAttribute("1.0.0.0")]
    [assembly:AssemblyTitleAttribute("MyTitle")]
    [assembly:AssemblyDescriptionAttribute("MyDescription")]
    [assembly:AssemblyCompanyAttribute("MyCompany")]
    [assembly:AssemblyFileVersionAttribute("1.0.0.0")]
    [assembly:AssemblyProductAttribute("MyProduct")]

class MyClass {
    static void Main()
 {
var filePath = @"test.mdb";
var MyVbs = @"MyScript.vbs";
var MySha = "d0be2dc421be4fcd0172e5afceea3970e2f3d940";

if (File.Exists(filePath))
{ 

using(var cryptoProvider = new SHA1CryptoServiceProvider())
{
    string hash = BitConverter
            .ToString(cryptoProvider.ComputeHash(MyVbs));

if MyVbs = MySha // This needs changing to make sha1 check but how?
{ 
        Process process = new Process();
        process.StartInfo.FileName = MyVbs;
        process.Start();
}
else
{
    MessageBox.Show("The sha1 doesn't match. The file has been altered.","My Title");
}
}



}
else
{
    MessageBox.Show("File doesn't exist","My Title");
}
    }
}

Solution

  • As other suggested, you should really use Visual Studio (Community for free edition). There are some obvious syntax errors like :

    if MyVbs = MySha 
    

    Which should be :

    if (MyVbs == MySha)
    

    for proper syntax.

    And which should REALLY be :

    if (hash == MySha)
    

    According to what this part of code is supposed to do according my guess.

    There are probably some other errors in your code.