小编Blo*_*ard的帖子

确定是否为绝对或相对URL

我在字符串中有一个相对或绝对的url.我首先需要知道它是绝对的还是相对的.我该怎么做呢?然后我想确定url的域是否在允许列表中.

这是我的允许列表,作为一个例子:

string[] Allowed =
{
   "google.com",
   "yahoo.com",
   "espn.com"
}
Run Code Online (Sandbox Code Playgroud)

一旦我知道它的相对或绝对,我认为它相当简单:

if (Url.IsAbsolute)
{
    if (!Url.Contains("://"))
        Url = "http://" + Url;

    return Allowed.Contains(new Uri(Url).Host);
}
else //Is Relative
{
    return true;
}
Run Code Online (Sandbox Code Playgroud)

c# url parsing

57
推荐指数
2
解决办法
3万
查看次数

Delphi:StringList Delimiter始终是空格字符,即使设置了Delimiter也是如此

我在TStringList类中使用分隔符时遇到问题.看一看:

var
  s: string;
  sl: TStringList;

begin
  sl := TStringList.Create;
  s := 'Users^foo bar^bar foo^foobar^barfoo';
  sl.Delimiter := '^';
  sl.DelimitedText := s;
  ShowMessage(sl[1]);
end;
Run Code Online (Sandbox Code Playgroud)

sl[1] 应该回来 'foo bar'

sl[1] 回来了 'foo'

看来分隔符现在是'^'AND' '

有任何想法吗?

delphi tstringlist delphi-7 delimiter

54
推荐指数
3
解决办法
7万
查看次数

什么是检查SQL Server中是否存在触发器的最便携方法?

我正在寻找最便携的方法来检查MS SQL Server中是否存在触发器.它至少需要处理SQL Server 2000,2005,最好是2008年.

信息似乎不在INFORMATION_SCHEMA中,但如果它在某处,我宁愿从那里使用它.

我知道这个方法:

if exists (
    select * from dbo.sysobjects 
    where name = 'MyTrigger' 
    and OBJECTPROPERTY(id, 'IsTrigger') = 1
) 
begin

end
Run Code Online (Sandbox Code Playgroud)

但我不确定它是否适用于所有SQL Server版本.

sql-server triggers information-schema

48
推荐指数
3
解决办法
7万
查看次数

在WPF(C#或vb.net)中查找应用程序可执行文件的位置?

如何在WPF(C#或VB.Net)中找到应用程序可执行文件的位置?

我在Windows窗体中使用了这段代码:

Application.ExecutablePath.ToString();
Run Code Online (Sandbox Code Playgroud)

但是使用WPF我从Visual Studio收到此错误:

System.Window.Application不包含ExecutablePath的定义.

c# vb.net wpf

47
推荐指数
4
解决办法
5万
查看次数

是否使用Mutex来防止同一程序的多个实例运行安全?

我正在使用此代码阻止我的程序的第二个实例同时运行,是否安全?

Mutex appSingleton = new System.Threading.Mutex(false, "MyAppSingleInstnceMutx");
if (appSingleton.WaitOne(0, false)) {
    Application.EnableVisualStyles();
    Application.SetCompatibleTextRenderingDefault(false);
    Application.Run(new MainForm());
    appSingleton.Close();
} else {
    MessageBox.Show("Sorry, only one instance of MyApp is allowed.");
}
Run Code Online (Sandbox Code Playgroud)

我担心如果有什么东西抛出一个异常并且应用程序崩溃了Mutex仍然会被保留.真的吗?

c# mutex single-instance winforms

45
推荐指数
4
解决办法
5万
查看次数

加速Python

这真的是两个问题,但是它们非常相似,为了保持简单,我想我只是把它们放在一起:

  • 首先:鉴于已经建立的python项目,除了简单的代码内优化之外,有什么方法可以加快速度?

  • 其次:在python中从头开始编写程序时,有哪些好方法可以大大提高性能?

对于第一个问题,想象一下你得到了一个写得很好的项目,你需要提高性能,但你似乎无法通过重构/优化获得很多收益.在这种情况下你会做些什么来加速它,而不是像C那样重写它?

python optimization performance

42
推荐指数
7
解决办法
2万
查看次数

是否有一个属性,我可以在我的类中使用它来告诉DataGridView在绑定到List <MyClass>时不为它创建一个列

我有一个这样的课:

private class MyClass {
  [DisplayName("Foo/Bar")]
  public string FooBar { get; private set; }
  public string Baz { get; private set; }      
  public bool Enabled;
}
Run Code Online (Sandbox Code Playgroud)

当我创建一个List<MyClass>并将其分配给DataSourcea时DataGridView,网格显示两列,"Foo/Bar"和"Baz".这就是我想要发生的事情.

它目前有效,因为Enabled是一个字段,而不是属性 - DataGridView只会获取属性.但是,这是一个肮脏的黑客.

我也想使Enabled成为一个属性,但仍然将它隐藏在DataGridView上.

我知道我可以在绑定后手动删除列..但这并不理想.

是否有类似于DisplayName的属性,我可以用?标记属性?有点像[Visible(false)]

c# attributes datagridview properties winforms

41
推荐指数
2
解决办法
2万
查看次数

在React子组件上使用props更新状态

我有一个React应用程序,其中父组件的道具传递给子组件,然后道具设置子项的状态.

将更新后的值发送到父组件后,子组件不会使用更新的props更新状态.

如何让它更新子组件的状态?

我的精简代码:

class Parent extends React.Component {
    constructor (props) {
        super(props);
        this.state = {name: ''} 
    }
    componentDidMount () {
        this.setState({name: this.props.data.name});
    }
    handleUpdate (updatedName) {
        this.setState({name: updatedName});
    }
    render () {
        return <Child name={this.state.name} onUpdate={this.handleUpdate.bind(this)} />
    }
}


class Child extends React.Component {
    constructor (props) {
        super(props);
        this.state = {name: ''} 
    }
    componentDidMount () {
        this.setState({name: this.props.name});
    }
    handleChange (e) {
        this.setState({[e.target.name]: e.target.value});
    }
    handleUpdate () {
        // ajax call that updates database with updated name and then …
Run Code Online (Sandbox Code Playgroud)

javascript reactjs

41
推荐指数
2
解决办法
3万
查看次数

为什么密码错误导致"填充无效且无法删除"?

我需要一些简单的字符串加密,所以我写了下面的代码(从这里有很多"灵感" ):

    // create and initialize a crypto algorithm
    private static SymmetricAlgorithm getAlgorithm(string password) {
        SymmetricAlgorithm algorithm = Rijndael.Create();
        Rfc2898DeriveBytes rdb = new Rfc2898DeriveBytes(
            password, new byte[] {
            0x53,0x6f,0x64,0x69,0x75,0x6d,0x20,             // salty goodness
            0x43,0x68,0x6c,0x6f,0x72,0x69,0x64,0x65
        }
        );
        algorithm.Padding = PaddingMode.ISO10126;
        algorithm.Key = rdb.GetBytes(32);
        algorithm.IV = rdb.GetBytes(16);
        return algorithm;
    }

    /* 
     * encryptString
     * provides simple encryption of a string, with a given password
     */
    public static string encryptString(string clearText, string password) {
        SymmetricAlgorithm algorithm = getAlgorithm(password);
        byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(clearText); …
Run Code Online (Sandbox Code Playgroud)

.net c# encryption exception

40
推荐指数
3
解决办法
7万
查看次数

如何注册document.onkeypress事件

我想使用javascript为文档注册keypress事件.

我用过:

document.attachEvent("onkeydown", my_onkeydown_handler);
Run Code Online (Sandbox Code Playgroud)

它适用于IE,但不适用于Firefox和Chrome.

我也尝试过:

document.addEventListener("onkeydown", my_onkeydown_handler, true); 
// (with false value also)
Run Code Online (Sandbox Code Playgroud)

但它仍然无法与Firefox和Chrome一起使用.

有解决方案吗,我错过了什么吗?

javascript events cross-browser

40
推荐指数
2
解决办法
7万
查看次数