|
这几天做一个通讯程序,要通过“运行”开启一个进程
结果有时候可以正常调用,有时候就出现问题
出问题代码如下:
Process p=Process.Start(t_path.Text.ToString(),t_file.Text.ToString());
Process.start()第一个参数是运行的程序名称,后面是该程序参数
如果这两个路径中不含空格的时候就可以正常运行,
如果包含像"D:\Program Files\"或者"G:\Documents and Settings"之类的路径程序就会出问题
原因是在运行行里面 如果运行的程序只接收一个参数,参数里面夹一个空格就相当于回车
也就是说,如果运行输入:c:\aa.exe d:\Program Files\tt.txt
其实运行的是:c:\aa.exe d:\Program
这样肯定会出错了
正确的写法应该是:
Process p=Process.Start("\""+t_path.Text.ToString()+"\"","\""+t_file.Text.ToString()+"\"");
这种写法绝对保险。
|
|
|
|
|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;
namespace Control.WPFControlLibrary
{
/// <summary>
/// IpControl.xaml 的交互逻辑
/// 一个IP的地址的输入控件,与系统中网络管理中的那个类似,直接托进来就可以用
/// </summary>
public partial class IpControl : UserControl
{
private string _text = "";
public IpControl()
{
InitializeComponent();
ChangeBackColor(this.textBox1.Background);
}
private void textBox_KeyUp(object sender, KeyEventArgs e)
{
MaskIpAddr((System.Windows.Controls.TextBox)sender, e);
}
bool checkkey(Key key)
{
if ((key >= Key.D0 && key <= Key.D9) || (key >= Key.NumPad0 && key <= Key.NumPad9))
return true;
else
return false;
}
private void MaskIpAddr(System.Windows.Controls.TextBox textBox, KeyEventArgs e)
{
int len = textBox.Text.Length;
if (checkkey(e.Key) || e.Key == Key.Decimal || e.Key == Key.OemPeriod || e.Key == Key.Back)
{
if (e.Key != Key.Back)
{
#region 处理按下的不是点并且是长度大于3或者等于3验证,并且向下一个textBox焦点
if (len == 2 && e.Key != Key.Decimal && e.Key != Key.OemPeriod)
{
//此处这么写会有问题,因为在长按的时候不会有UP事件。
string tmp = textBox.Text + (e.Key.GetHashCode() - 34);
if (textBox.Name == "textBox1")
{
if (Int32.Parse(tmp) > 223) // 进行验证
{
MessageBox.Show(tmp + " 不是一个有效IP项。请指定一个介于 1 和 223 之间的数值。");
textBox.Text = "";
textBox.Text = "223";
textBox.Focus();
textBox.Select(0, textBox.Text.Length);
e.Handled = true;
return;
}
textBox.Text = tmp;
e.Handled = true;
textBox2.Focus();
textBox2.SelectAll();
}
else if (textBox.Name == "textBox2")
{
if (Int32.Parse(tmp) > 255)
{
MessageBox.Show(tmp + " 不是一个有效IP项。请指定一个介于 1 和 255 之间的数值。");
textBox.Text = "";
textBox.Text = "255";
textBox.Focus();
textBox.Select(0, textBox.Text.Length);
e.Handled = true;
return;
}
textBox.Text = tmp;
e.Handled = true;
textBox3.Focus();
textBox3.SelectAll();
}
else if (textBox.Name == "textBox3")
{
if (Int32.Parse(tmp) > 255)
{
MessageBox.Show(tmp + " 不是一个有效IP项。请指定一个介于 1 和 255 之间的数值。");
textBox.Text = "";
textBox.Text = "255";
textBox.Focus();
textBox.Select(0, textBox.Text.Length);
e.Handled = true;
return;
}
textBox.Text = tmp;
e.Handled = true;
textBox4.Focus();
textBox4.SelectAll();
}
else if (textBox.Name == "textBox4")
{
if (Int32.Parse(tmp) > 255)
{
MessageBox.Show(tmp + " 不是一个有效IP项。请指定一个介于 1 和 255 之间的数值。");
textBox.Text = "";
textBox.Text = "255";
textBox.Select(0, textBox.Text.Length);
textBox.Focus();
e.Handled = true;
return;
}
//textBox.Text = tmp;
//textBox4.Focus();
//textBox4.SelectAll();
}
}
#endregion
#region 处理是点的情况 true
if (e.Key == Key.Decimal || e.Key == Key.OemPeriod)
{
if (textBox.Name == "textBox1" && textBox.Text != "")
{
textBox2.Focus();
textBox2.SelectAll();
}
if (textBox.Name == "textBox2" && textBox.Text != "")
{
textBox3.Focus();
textBox3.SelectAll();
}
if (textBox.Name == "textBox3" && textBox.Text != "")
{
textBox4.Focus();
textBox4.SelectAll();
}
if (textBox.Name == "textBox4" && textBox.Text != "")
{
}
e.Handled = true;
}
#endregion
}
else //
{
if (textBox.Name == "textBox1" && textBox.Text == "")
{
}
if (textBox.Name == "textBox2" && textBox.Text == "")
{
textBox1.Focus();
textBox1.SelectionStart = textBox1.Text.Length;
}
if (textBox.Name == "textBox3" && textBox.Text == "")
{
textBox2.Focus();
textBox2.SelectionStart = textBox2.Text.Length;
}
if (textBox.Name == "textBox4" && textBox.Text == "")
{
textBox3.Focus();
textBox3.SelectionStart = textBox3.Text.Length;
}
e.Handled = false;
}
}
else
{
//if (ss.Length > 0)
//{
// textBox.Text = ss.Substring(0,(ss.Length-1));
//}
e.Handled = true;
}
}
//[Browsable(true)]
// 获取 IpBox 的文本。
public string Text
{
get
{
if (textBox1.Text == ""
|| textBox2.Text == ""
|| textBox3.Text == ""
|| textBox4.Text == "")
{
_text = "";
return _text;
}
else
{
_text = Convert.ToInt32(textBox1.Text).ToString() + "." + Convert.ToInt32(textBox2.Text).ToString() + "." + Convert.ToInt32(textBox3.Text).ToString() + "." + Convert.ToInt32(textBox4.Text).ToString();
return _text;
}
}
set //
{
if(value != null)
{
// 255-255-255-255
//string pattern = @"^([1-9]|\d{2}|1[0-9]{1,2}|2[0-5]{2})\.(\d{1,2}|1[0-9]{1,2}|[1-2][0-5]{2})\.(\d{1,2}|1[0-9]{1,2}|[1-2][0-5]{2})\.(\d{1,2}|1[0-9]{1,2}|[1-2][0-5]{2})$";
// 223-255-255-255
string pattern = @"^([1-9]|\d{2}|1[0-9]{1,2}|2[1-2][1-3])\.(\d{1,2}|1[0-9]{1,2}|[1-2][0-5]{2})\.(\d{1,2}|1[0-9]{1,2}|[1-2][0-5]{2})\.(\d{1,2}|1[0-9]{1,2}|[1-2][0-5]{2})$";
if( !Regex.IsMatch(value , pattern) )
{
_text = "";
throw new Exception("IP 地址格式错误!");
}
else
{
string[] ipnum = value.Split('.');
textBox1.Text = ipnum[0];
textBox2.Text = ipnum[1];
textBox3.Text = ipnum[2];
textBox4.Text = ipnum[3];
}
}
_text = value;
}
}
private void textBox1_GotFocus(object sender, RoutedEventArgs e)
{
if (sender.GetType() == typeof(TextBox))
ChangeBackColor(((TextBox)sender).Background);
else
ChangeBackColor(((Label)sender).Background);
}
private void ChangeBackColor(Brush tb)
{
this.dockPanel1.Background = tb;
this.label1.Background = tb;
this.label2.Background = tb;
this.label3.Background = tb;
this.textBox1.Background = tb;
this.textBox2.Background = tb;
this.textBox3.Background = tb;
this.textBox4.Background = tb;
}
private void textBox1_MouseEnter(object sender, MouseEventArgs e)
{
if (sender.GetType()==typeof(TextBox))
ChangeBackColor(((TextBox)sender).Background);
else
ChangeBackColor(((Label)sender).Background);
}
private void textBox1_GotFocus(object sender, MouseButtonEventArgs e)
{
}
}
}
XAML
<UserControl x:Class="Control.WPFControlLibrary.IpControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="23" Width="125">
<UserControl.Resources>
<Style x:Key="LableStyle" TargetType="{x:Type Label}">
<Setter Property="Template">
<Setter.Value>
<!--设置样式 -->
<ControlTemplate TargetType="{x:Type Label}">
<Grid>
<Rectangle x:Name="Rectangle" Stroke="#FFFFFFFF" StrokeMiterLimit="1.000000" StrokeThickness="0.500000" RadiusX="10" RadiusY="10" Fill="#FF777777">
</Rectangle>
<ContentPresenter x:Name="ContentPresenter" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalAlignment="{TemplateBinding VerticalContentAlignment}" RecognizesAccessKey="True"/>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</UserControl.Resources>
<Border BorderThickness="1" BorderBrush="LightBlue" HorizontalAlignment="Left" VerticalAlignment="Top" Height="23">
<DockPanel Name="dockPanel1" Height="23" VerticalAlignment="Top">
<TextBox Name="textBox1" MaxLength="3" MouseEnter ="textBox1_MouseEnter" GotFocus="textBox1_GotFocus" KeyDown="textBox_KeyUp" Width="28" Height="23" BorderThickness="0" TextAlignment="Center" VerticalContentAlignment="Center" />
<Label Height="4" MouseEnter ="textBox1_MouseEnter" Name="label1" Style="{StaticResource LableStyle}" Width="4" Margin="0,0,0,0" VerticalContentAlignment="Center">.</Label>
<TextBox MouseEnter ="textBox1_GotFocus" MaxLength="3" Height="23" Name="textBox2" KeyDown="textBox_KeyUp" Width="28" BorderThickness="0" TextAlignment="Center" VerticalContentAlignment="Center"/>
<Label Height="4" Name="label2" MouseEnter ="textBox1_MouseEnter" Style="{StaticResource LableStyle}" Width="4" Background="Transparent" VerticalContentAlignment="Center">.</Label>
<TextBox MouseEnter ="textBox1_GotFocus" MaxLength="3" Height="23" Name="textBox3" KeyDown="textBox_KeyUp" Width="28" BorderThickness="0" TextAlignment="Center" VerticalContentAlignment="Center"/>
<Label Height="4" Name="label3" GotMouseCapture="textBox1_MouseEnter" Style="{StaticResource LableStyle}" Width="4" Background="Transparent" VerticalContentAlignment="Top">.</Label>
<TextBox MouseEnter ="textBox1_GotFocus" MaxLength="3" GotMouseCapture="textBox1_MouseEnter" Height="23" Name="textBox4" KeyDown="textBox_KeyUp" Width="28" BorderThickness="0" TextAlignment="Center" VerticalContentAlignment="Center"/>
</DockPanel>
</Border>
</UserControl>
|
|
|
|