[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2013-06-13
28
Medium Priority
?
400 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

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

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

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

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

649 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