Home > Net >  In c # Windows service program, how to use the shell executive net use command.
In c # Windows service program, how to use the shell executive net use command.

Time:09-16

How to use c #, VB.NET can write?

Shared Function checkPathExists (ByVal PathStr As String) As Boolean
Dim As String s="CMD. Exe/c net use" + "" + + NetUsePath CRH (34) + PWD + CRH (34) +"/user: "+ CRH (34) + userName + CRH (34)
My.Com puter. FileSystem. WriteAllText (My. Application. Info. DirectoryPath & amp; "\ logfile" & amp; "\ TTT. TXT" and Strings. The Format (Now, "yyyy/MM/dd HH: MM: ss") & amp; "" & amp; S True, System. Text. Encoding. GetEncoding (" GB2312 "))

Shell (s, 1)
Thread.sleep (500)
If System. IO. Directory. The Exists (PathStr)=False Then
CheckPathExists=False
The Else
CheckPathExists=True
End the If
End the Function


c # code, execute invalid, always return false

Private bool NetUse ()
{
Process Process=new Process ();
//set the program name
Process. StartInfo. FileName="CMD. Exe";

//the use of closed Shell
Process. StartInfo. UseShellExecute=false;
////redirect standard input, the input and output error
Process. StartInfo. RedirectStandardInput=true;
Process. StartInfo. RedirectStandardOutput=true;
Process. StartInfo. RedirectStandardError=true;

//set the CMD window does not display the
Process. StartInfo. CreateNoWindow=false;
//
Process. The Start ();
//type the command from the
String CMD=@ "net use" + "" + + ClassConfig.net Path ClassConfig. PWD +"/user: admin ";
Process. The StandardInput. WriteLine (CMD);
//net use \ \ plant1 - test \ Shared "plant1/user" : "Asia \ user
"//net use \ \ 192.168.0.4 admin1234/user: admin

Process. The StandardInput. WriteLine (" exit ");
System.Threading.Thread.Sleep(1000);
If (Directory. The Exists Path (ClassConfig.net)==false)
{
System. IO. File. AppendAllText (Application. StartupPath + "\ \ ErrNETUSE TXT," Directory. The Exists Path (ClassConfig.net) + "\ n");
return false;
}
The else
{
System. IO. File. AppendAllText (Application. StartupPath + "\ \ ErrNETUSE TXT," Directory. The Exists Path (ClassConfig.net) + "\ n");
return true;
}
}

CodePudding user response:

Very anxious! Please help to test, also is the service service program, thank humbly!

CodePudding user response:

Don't want to turn over the code, you baidu "Windows service session0 rights"

https://www.baidu.com/s? Wd=20 service window % % 20 session0 90% % 20% % E6 8 f % E6%9 d % 83 & amp; Rsv_spt=1 & amp; Rsv_iqid=0 x8aaefba600045580 & amp; Issp=1 & amp; f=8& rsv_bp=1& Rsv_idx=2 & amp; ie=utf-8& Tn=baiduhome_pg & amp; Rsv_enter=1 & amp; Rsv_dl=ib& Rsv_n=2 & amp; Rsv_sug3=1

CodePudding user response:

Thank you for such a fast back to me, the more I learn about your hair connection, there is a problem still please teach you, thank you!

CodePudding user response:

https://stackoverflow.com/questions/281951/net-use-command-in-a-windows-service

Here are explained, why don't you ask rights, could not be used

CodePudding user response:

Is too high,
I the net to use the network location, and built on a network backup folder, create a file,
It doesn't come out,

CodePudding user response:

Can't, we also don't know how Microsoft after vista, check your code before vista simply allow interaction on ok
But after vista out a session0 state, even if the check is invalid, but for such a right way, so that you can do, is no words

CodePudding user response:

If you feel session0 right code is not good, you can be as simple processing,

Written in bat batch or powshell directly, after the console or standard to write, write on the startup

Or use the program task creation background tasks,

CodePudding user response:

Microsoft actually official advice is to write the initiative task
Such as the following
https://wenku.baidu.com/view/aeaea00190c69ec3d5bb7512

But planning tasks, easily deleted by the user himself, so we are less common

CodePudding user response:

Ok, thank you, help to reply you so late, tomorrow I think it over, and see how, thank you once again
  •  Tags:  
  • C#
  • Related