-3

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");
}
    }
}
p deman
  • 75
  • 3
  • 11
  • 3
    I strongly recommend using Visual Studio Community. It'll give you clearer error messages and you'll have easier time learning. – Ori Nachum Mar 13 '17 at 13:41
  • I get the following error, is that what you're seeing? `test.cs(30,4): error CS1003: Syntax error, '(' expected test.cs(30,17): error CS1026: ) expected` – GER Mar 13 '17 at 13:51
  • hello GER yes that is the error. However this line “if MyVbs = MySha “ which I have commented needs to be changed to do the sha1 check but I don’t know how. This is much more than just a compiling issue I’m posting it’s a how to get my code to do what I have said question and any help would be very much appreciated – p deman Mar 13 '17 at 14:03
  • Please to everyone in the interests of learning if anyone knows the correct code solution to my posted code example to get the sha1 of @"MyScript.vbs" and check it against MySha I would be grateful. I may further down the lines install all kinds of IDE’s and programs and join all kinds of communities to help me further understand C# but please for now let me know the answer to my question. – p deman Mar 13 '17 at 14:12
  • 2
    pderman - fyi - StackOverflow is not a collaborative teaching or "just give me working code that does what I want" site.. Instead, it is focused about specific questions that can help you and others. Your question seems to be about working with SHA, but in fact it is not. Code you provided depends on VBS script for computing SHA, and this piece of script is hidden, so even that part will not help anyone interested in SHA.. I'm almost sure that **this** question will be put on hold. It does not mean "closed" yet. It means you need to **change it** and make more specific and less broad. – quetzalcoatl Mar 13 '17 at 14:38
  • If it gets closed, don't get angry or discouraged. Simply 'edit' it and fix the question, or ask another one (maybe deleting this one in the if you don't want to correct this question). Important thing is to ask about one specific thing, and (usually) "how to get it working" is not specific, is not one, and often, neither is a "thing" - because if somebody asks like that, then usually (s)he lacks some ideas behind what (s)he tries to write. – quetzalcoatl Mar 13 '17 at 14:45
  • In this case, you've said you tried many SHA implementations. Why did you settle on VBS script? It's odd. You probably just got yourself more problems with it. You will need to connect to scripting engine, or ran it as Process (you started to code that part). Please, find a SHA impl that is written in C#. Here, please see [this question](http://stackoverflow.com/questions/1993903/how-do-i-do-a-sha1-file-checksum-in-c) - it uses [SHA1Managed class from .Net itself](https://msdn.microsoft.com/en-us/library/system.security.cryptography.sha1managed.sha1managed(v=vs.110).aspx) no extra libs needed. – quetzalcoatl Mar 13 '17 at 14:49
  • Whoa. I just noticed that you mean to run that VBS script if MDB file is changed. I don't know where I did get an idea that this VBS script is your SHA implementation, sorry. Maybe that `if MyVbs = MySha` got me fooled. I removed my comment about running VBS where I could, some other I could not edit anymore, so please disregard that part. Anyways, this does not change what I wrote later - see that example, try to use that SHA-calculating piece, and, well, report back with new questions, if any. – quetzalcoatl Mar 13 '17 at 15:03

1 Answers1

0

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.

AFract
  • 8,868
  • 6
  • 48
  • 70
  • AFract I tried if (hash == MySha) and I get the following error:- splash.cs(28,23): error CS1502: The best overloaded method match for 'System.Security.Cryptography.HashAlgorithm.ComputeHash(System.IO.Stream )' has some invalid arguments splash.cs(28,50): error CS1503: Argument '1': cannot convert from 'string' to 'System.IO.Stream' – p deman Mar 13 '17 at 14:18
  • And what you did to fix it ? Have you only tried to understood why it complains about that, and search on your favorite search engine how to compute SHA1 from a string or a file ? – AFract Mar 13 '17 at 14:36
  • I am searching desperately to find the solution to my code and I try them all but none work and all I am left with are more problems and still my code doesn’t work. If you or anyone knows how to make my code work I really would be grateful if you could post it. It would be a big learning curve for me. – p deman Mar 13 '17 at 14:45
  • Glad to be helpful. Don't hesitate to click on the green check at left of my post to mark you're question as resolved, this will allow it to be removed of "unanswered" questions list – AFract Mar 16 '17 at 13:13