C# Windows身份验证成功后运行Excel文件
我试图整理一些代码,要求用户输入当前的windows用户名和密码,以便运行excel文件。但是,我的代码的process.start部分有问题。不知道我错过了什么 代码:C# Windows身份验证成功后运行Excel文件,c#,visual-studio,if-statement,process.start,C#,Visual Studio,If Statement,Process.start,我试图整理一些代码,要求用户输入当前的windows用户名和密码,以便运行excel文件。但是,我的代码的process.start部分有问题。不知道我错过了什么 代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using Sy
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace TestApp
{
public partial class Form1 : Form
{
[System.Runtime.InteropServices.DllImport("advapi32.dll")]
public static extern bool LogonUser(string userName, string domainName, string password, int LogonType, int LogonProvider, ref IntPtr phToken);
public Form1()
{
InitializeComponent();
}
private void btnLogin_Click(object sender, EventArgs e)
{
bool issuccess = false;
string username = GetloggedinUserName();
if (username.ToLowerInvariant().Contains(txtUserName.Text.Trim().ToLowerInvariant()) && username.ToLowerInvariant().Contains(txtDomain.Text.Trim().ToLowerInvariant()))
{
issuccess = IsValidateCredentials(txtUserName.Text.Trim(), txtPwd.Text.Trim(), txtDomain.Text.Trim());
}
if (issuccess)
MessageBox.Show("Successfuly Login !!!");
// Here is where I am having trouble. I cannot get this file to run.
Process.Start("Z:\\FolderA\\Test.xlsx");
else
MessageBox.Show("Invalid Username and/or Password Combination. Please try again. ");
}
private string GetloggedinUserName()
{
System.Security.Principal.WindowsIdentity currentUser = System.Security.Principal.WindowsIdentity.GetCurrent();
return currentUser.Name;
}
private bool IsValidateCredentials(string userName, string password, string domain)
{
IntPtr tokenHandler = IntPtr.Zero;
bool isValid = LogonUser(userName, domain, password, 2, 0, ref tokenHandler);
return isValid;
}
private void ButtonCancel_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
- Visual Studio错误消息:
错误CS0103当前上下文TestApp C:\C\#\Windows身份验证\C\TestApp\Form1.cs中不存在名称“Process” 错误CS1513}应为TestApp C:\C#\Windows身份验证\C#\TestApp\Form1.cs
您的
IF
语句中缺少大括号:
if (issuccess)
{
MessageBox.Show("Successfuly Login !!!");
Process.Start("Z:\\FolderA\\Test.xlsx");
}
else
{
MessageBox.Show("Invalid Username and/or Password Combination. Please try again. ");
}
对于多行IF语句,始终使用大括号是一条安全的规则。请添加您收到的确切错误消息/异常。另外,请澄清您的实际代码是否编译,并确保您在问题中发布的内容反映了您的实际问题。到目前为止,代码在语法上是不正确的。旁注:请不要在标题中添加“谢谢”注释、您的生活故事(这里是新的/语言是新的)和标记。@AlexeiLevenkov-最重要的是,我认为在讨论之前的内容和欣赏时是恰当的。偶然地,在你陈述的前一部分:我在之前的一篇帖子中因为提供错误消息而受到指责,所以…我有点困惑。坦率地说,但出于尊重,我意识到我的代码“语法不正确”。这就是我请求帮助的原因。无论如何,谢谢你的建设性批评。请注意:如果我删除进程启动,它将编译请使用
System.Diagnostics.Process.Start(“”)进行检查代码>