?
Solved

How to create a EXE to run command in Visual Studio 2012

Posted on 2013-06-13
28
Medium Priority
?
402 Views
Last Modified: 2013-08-21
Greeting,

I have the following batch file which will run a program in Windows 7 and  I want to encrypt the MyUserName and Password. I know I can create a EXE file in VS2012 which will run the command and display message "Success" or "Failed" when it is done. Please provide detailed instruction.

cd %ProgramFiles(X86)%\xxxxxxx\bin
zac unr -u MyUserName -p MyPassword
pause
exit

Thanks in advance.
0
Comment
Question by:mrong
  • 13
  • 10
  • 5
28 Comments
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 39244565
assume u have the batch file saved as file.bat, u can create simple console applciation using visual studio with the following code:
            Process proc = new Process();
            int timeout = 10000;
            proc.StartInfo.FileName = "file.bat";
            proc.StartInfo.RedirectStandardError = true;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.UseShellExecute = false;

            proc.Start();
            proc.WaitForExit(timeout);

            string errorMessage = proc.StandardError.ReadToEnd();
            proc.WaitForExit();
            if (errorMessage == string.Empty)
            {
                Console.WriteLine("Success");
            }
            else
            {
                Console.WriteLine("Failed: " + errorMessage);
            }

Open in new window

the timeout is optional and currently set to 10 seconds but u can remove it if not required.
0
 

Author Comment

by:mrong
ID: 39244597
so this console application need to be put into the same folder as the batch file? If that is the case, the user can still open the batch file to see the username & password which is what I want to avoid.

Thanks.
0
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 39244604
it doesn't have to be in the same folder, it can be anywhere in the file system as long as u put the correct bat.file location in your code.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

Author Comment

by:mrong
ID: 39244615
my bat file will open a dos prompt to exe the command(shows the username and password). Will your suggestion still open the dos prompt?

Thanks.
0
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 39244633
the code simply runs the batch file and output result, it doesn't interfere with its implementation (if that's what u asking).
0
 

Author Comment

by:mrong
ID: 39244640
Can I include the bat file in the application and compile into one package?
Thanks.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39244651
Why do you need a whole new application just to print "succes" or "failed"? Can't you just do this in the batch file?
0
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 39244653
what do u mean package?
like installer?
you actually create the batch file from the you console app and run it right after so the user won't have access to it.
0
 

Author Comment

by:mrong
ID: 39244665
Yes, the installer.
0
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 39244684
so u need to create setup project using visual studio and add the exe of the console application to the installer package.
0
 

Author Comment

by:mrong
ID: 39244692
ok, I am creating the console application now. But gave me syntax error. Did I do anything wrong?

Module Module1

    Sub Main()
        Process proc = new Process();
            int timeout = 10000;
            proc.StartInfo.FileName = "file.bat";
            proc.StartInfo.RedirectStandardError = true;
            proc.StartInfo.RedirectStandardOutput = true;
            proc.StartInfo.UseShellExecute = false;

            proc.Start();
            proc.WaitForExit(timeout);

            string errorMessage = proc.StandardError.ReadToEnd();
            proc.WaitForExit();
            if (errorMessage == string.Empty)
            {
                Console.WriteLine("Success");
            }
        Else
            {
                Console.WriteLine("Failed: " + errorMessage);
            }
    End Sub

End Module
0
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 39244701
which line?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39244710
So to expand, your creating 15+ lines of code to do what 2 can do?

e.g.

cd %ProgramFiles(X86)%\xxxxxxx\bin
zac unr -u MyUserName -p MyPassword

IF ERRORLEVEL = 0 ECHO Success
ELSE ECHO Failed

pause
exit

Open in new window


Is there any particular reason why?

P.S.

Curly braces aren't used in VB in that way (If/Then).
0
 

Author Comment

by:mrong
ID: 39244712
They are all over the place...

'Process' is a type and cannot be used as expression.

'proc' is not declared
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39244719
You're mixing C# syntax with VB.NET syntax.
0
 
LVL 42

Accepted Solution

by:
Meir Rivkin earned 2000 total points
ID: 39244726
its C# and u need vb.net so here:
Dim proc As New Process()
Dim timeout As Integer = 10000
proc.StartInfo.FileName = "file.bat"
proc.StartInfo.RedirectStandardError = True
proc.StartInfo.RedirectStandardOutput = True
proc.StartInfo.UseShellExecute = False

proc.Start()
proc.WaitForExit(timeout)

Dim errorMessage As String = proc.StandardError.ReadToEnd()
proc.WaitForExit()
If errorMessage = String.Empty Then
	Console.WriteLine("Success")
Else
	Console.WriteLine("Failed: " & errorMessage)
End If

Open in new window

0
 

Author Comment

by:mrong
ID: 39244734
kaufmed,

My batch file run a program with supplied username and password in dos prompt. Here is what I need: I don't want the username and password show in the dos prompt, neither anyone can open the batch to see the username and password. I want to encrypt the username and password.

I thought I can create EXE to have everything embeded.

Thanks.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39244738
You do realize that unless you obfuscate your code and encrypt any strings, anyone can download a decompiler and open up your newly-created .exe and see your code, yes?

Example
0
 

Author Comment

by:mrong
ID: 39244772
kaufmed,

oh....that's right:(

How hard is it to obfuscate the code and encrypt any strings? lots of work?

Thanks.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39244800
I haven't used any of the obfuscators, so I'll have to defer to the others. I know that a "community edition" of DotFuscator comes with VS (not Express edition, I believe).

As far as encryption, who are you trying to keep out? Average Joe, or Joe Hacker? You've got an issue that you are deploying this code to a system that you have no control over. If you embed the encryption key into the program, you've got the same issue with security. Joe Hacker can get at your strings, but Average Joe probably won't know what you're doing. You could probably just use an XOR "encryption" to prevent Average Joe from seeing the strings.
0
 

Author Comment

by:mrong
ID: 39244805
I tried to complie the app but got an error can't find file.

    proc.StartInfo.FileName = "c:\file.bat";
0
 

Author Comment

by:mrong
ID: 39244814
kaufmed,

This is for internal use. I guess as long as put into a EXE should be fine.
Thanks.
0
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 39244815
did u create the batch file as file.bat and saved under c:?
0
 

Author Comment

by:mrong
ID: 39244868
Ok, I keep the timeout to 10 sec. When I run the app, the dos window just stay on my screen forever .....

thanks.
0
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 39244880
add this to the code:
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

Open in new window

0
 

Author Comment

by:mrong
ID: 39244902
I changed to the following but the dos window still stay on my screen.

Sub Main()
        Dim proc As New Process()
        Dim timeout As Integer = 10000
        proc.StartInfo.CreateNoWindow = True
        proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden
        proc.StartInfo.FileName = "c:/MyFile.bat"
        proc.StartInfo.RedirectStandardError = True
        proc.StartInfo.RedirectStandardOutput = True
        proc.StartInfo.UseShellExecute = False

       

        proc.Start()
        proc.WaitForExit(timeout)

        Dim errorMessage As String = proc.StandardError.ReadToEnd()
        proc.WaitForExit()
        If errorMessage = String.Empty Then
            Console.WriteLine("Success")
        Else
            Console.WriteLine("Failed: " & errorMessage)
        End If
    End Sub
0
 
LVL 42

Expert Comment

by:Meir Rivkin
ID: 39244922
did u try without the timeout?
0
 

Author Comment

by:mrong
ID: 39244940
Yes, same thing.
thanks.
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question