Solved

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

Posted on 2013-06-13
28
391 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:sedgwick
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:sedgwick
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
 

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:sedgwick
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 74

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:sedgwick
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:sedgwick
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:sedgwick
ID: 39244701
which line?
0
 
LVL 74

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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 74

Expert Comment

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

Accepted Solution

by:
sedgwick earned 500 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 74

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 74

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:sedgwick
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:sedgwick
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:sedgwick
ID: 39244922
did u try without the timeout?
0
 

Author Comment

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

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now