C# 检查网络c上是否存在目录#

C# 检查网络c上是否存在目录#,c#,jquery,C#,Jquery,我试图根据用户的输入字段查看目录是否存在。当用户键入路径时,我想检查路径是否确实存在 我已经有一些C代码了。它总是返回0,除了字符串“C:\Program Files” 感谢您的帮助。谢谢 编辑4:终于找到了解决方案!见下面的答案。BrokenGlass的代码也会起作用。我发现只添加一个web.config文件更快 编辑3:更新代码以使用BrokenGlass的模拟功能。还是没有运气 编辑2:我更新了代码,尝试使用模拟,如下所示。它仍然每次都失败。我假设我不正确地使用模拟 编辑:根据ChrisF

我试图根据用户的输入字段查看目录是否存在。当用户键入路径时,我想检查路径是否确实存在

我已经有一些C代码了。它总是返回0,除了字符串“C:\Program Files”

感谢您的帮助。谢谢

编辑4:终于找到了解决方案!见下面的答案。BrokenGlass的代码也会起作用。我发现只添加一个web.config文件更快

编辑3:更新代码以使用BrokenGlass的模拟功能。还是没有运气

编辑2:我更新了代码,尝试使用模拟,如下所示。它仍然每次都失败。我假设我不正确地使用模拟

编辑:根据ChrisF的请求,下面是调用checkValidPath函数的函数

前端aspx文件

$.get('processor.ashx', { a: '7', path: x }, function(o) {
            alert(o);
            if (o=="0") {
                $("#outputPathDivValid").dialog({
                    title: 'Output Path is not valid! Please enter a path that exists!',
                    width: 500,
                    modal: true,
                    resizable: false,
                    buttons: {
                        'Close': function() { $(this).dialog('close'); }
                    }
                });
            }
        });
后端ashx文件

public void ProcessRequest (HttpContext context) {
    context.Response.Cache.SetExpires(DateTime.Now);
    string sSid = context.Request["sid"];
    switch (context.Request["a"])
    {//a bunch of case statements here...
            case "7":
            context.Response.Write(checkValidPath(context.Request["path"].ToString()));
            break;
所有信息都在这里:

例如:

Directory.Exists(myPath);
所有信息都在这里:

例如:

Directory.Exists(myPath);
试一试

您的代码也适用于我,请确保传递完整路径,即
@“C:\myDir\myDir2”
而不是
“myDir2”

要模拟网络路径的用户,请尝试以下操作:

using(ImpersonateUser user = new ImpersonateUser(user, "", password))
{
     bool doesExist = Directory.Exists(networkPath);
}
这基于以下帮助器类:

public class ImpersonateUser : IDisposable
{
    [DllImport("advapi32.dll", SetLastError = true)]
    private static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, out IntPtr phToken);

    [DllImport("kernel32", SetLastError = true)]
    private static extern bool CloseHandle(IntPtr hObject);

    private IntPtr userHandle = IntPtr.Zero;
    private WindowsImpersonationContext impersonationContext;

    public ImpersonateUser(string user, string domain, string password)
    {
        if (!string.IsNullOrEmpty(user))
        {
            // Call LogonUser to get a token for the user
            bool loggedOn = LogonUser(user, domain, password,
                    9 /*(int)LogonType.LOGON32_LOGON_NEW_CREDENTIALS*/,
                    3 /*(int)LogonProvider.LOGON32_PROVIDER_WINNT50*/,
                    out userHandle);
            if (!loggedOn)
                throw new Win32Exception(Marshal.GetLastWin32Error());

            // Begin impersonating the user
            impersonationContext = WindowsIdentity.Impersonate(userHandle);
        }
    }

    public void Dispose()
    {
        if (userHandle != IntPtr.Zero)
            CloseHandle(userHandle);
        if (impersonationContext != null)
            impersonationContext.Undo();
    }
}
试一试

您的代码也适用于我,请确保传递完整路径,即
@“C:\myDir\myDir2”
而不是
“myDir2”

要模拟网络路径的用户,请尝试以下操作:

using(ImpersonateUser user = new ImpersonateUser(user, "", password))
{
     bool doesExist = Directory.Exists(networkPath);
}
这基于以下帮助器类:

public class ImpersonateUser : IDisposable
{
    [DllImport("advapi32.dll", SetLastError = true)]
    private static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, out IntPtr phToken);

    [DllImport("kernel32", SetLastError = true)]
    private static extern bool CloseHandle(IntPtr hObject);

    private IntPtr userHandle = IntPtr.Zero;
    private WindowsImpersonationContext impersonationContext;

    public ImpersonateUser(string user, string domain, string password)
    {
        if (!string.IsNullOrEmpty(user))
        {
            // Call LogonUser to get a token for the user
            bool loggedOn = LogonUser(user, domain, password,
                    9 /*(int)LogonType.LOGON32_LOGON_NEW_CREDENTIALS*/,
                    3 /*(int)LogonProvider.LOGON32_PROVIDER_WINNT50*/,
                    out userHandle);
            if (!loggedOn)
                throw new Win32Exception(Marshal.GetLastWin32Error());

            // Begin impersonating the user
            impersonationContext = WindowsIdentity.Impersonate(userHandle);
        }
    }

    public void Dispose()
    {
        if (userHandle != IntPtr.Zero)
            CloseHandle(userHandle);
        if (impersonationContext != null)
            impersonationContext.Undo();
    }
}
对于
Exists
属性,请从中选择:

如果在尝试确定指定文件是否存在时发生任何错误,Exists属性将返回false。这可能发生在引发异常的情况下,例如传递的文件名包含无效字符或太多字符,磁盘出现故障或丢失,或者调用方没有读取文件的权限

那么,以下任何一项是否适用:

  • 路径是否包含任何无效字符
  • 是不是太长了
  • 它真的描述了一条存在的路径吗
  • 进程的所有者是否具有读取文件的权限
  • 第4点很重要。虽然“您”作为开发人员在本地测试应用程序时可能拥有权限,但您需要确保在服务器上运行该程序的帐户具有权限。如果没有,应用程序将失败,并且当您尝试重复该问题时,应用程序不会明显出现。

    对于
    Exists
    属性:

    如果在尝试确定指定文件是否存在时发生任何错误,Exists属性将返回false。这可能发生在引发异常的情况下,例如传递的文件名包含无效字符或太多字符,磁盘出现故障或丢失,或者调用方没有读取文件的权限

    那么,以下任何一项是否适用:

  • 路径是否包含任何无效字符
  • 是不是太长了
  • 它真的描述了一条存在的路径吗
  • 进程的所有者是否具有读取文件的权限

  • 第4点很重要。虽然“您”作为开发人员在本地测试应用程序时可能拥有权限,但您需要确保在服务器上运行该程序的帐户具有权限。如果不这样做,应用程序将失败,并且当您尝试重复此问题时,它将不明显。

    返回False,因为属性“Exists”表示目录是否存在-因此,如果您将文件路径作为参数传递给
    DirectoryInfo
    构造函数,将返回False。如果使用作为参数传递的现有目录创建
    DirectoryInfo
    ,则会得到一个true。 若要确定文件是否存在,应使用以下命令进行检查:
    file.exists

    因此,您确定用户输入的路径指向现有目录(不是文件)吗?

    返回False,因为属性“Exists”表示如果目录存在-因此,如果将文件路径作为参数传递给
    DirectoryInfo
    构造函数,将返回False。如果使用作为参数传递的现有目录创建
    DirectoryInfo
    ,则会得到一个true。 若要确定文件是否存在,应使用以下命令进行检查:
    file.exists

    那么,您确定用户输入的路径指向现有目录(不是文件)吗?

    因此我找到了解决方案。我只是将web.config文件中的模拟添加到web应用程序所在的文件夹中。这是我在文件中使用的代码

    <configuration>
    <appSettings/>
    <connectionStrings/>
    <system.web>
        <identity impersonate="true" userName="registry:HKLM\Software\medc\sys_content_pub,userName" password="registry:HKLM\Software\medc\sys_content_pub,password"/>
    </system.web>
    

    所以我找到了解决办法。我只是将web.config文件中的模拟添加到web应用程序所在的文件夹中。这是我在文件中使用的代码

    <configuration>
    <appSettings/>
    <connectionStrings/>
    <system.web>
        <identity impersonate="true" userName="registry:HKLM\Software\medc\sys_content_pub,userName" password="registry:HKLM\Software\medc\sys_content_pub,password"/>
    </system.web>
    

    您所拥有的一切看起来都是正确的,您是否验证了您在函数中接收到的内容?请记住,当您的函数返回时,答案可能已过时。文件系统是一个共享资源,其他进程可以随时访问它。Path参数是正确的文件路径吗?这可能是你的问题。你为什么返回字符串?@SLaks我返回字符串是为了测试。稍后我会将函数更改为bool或int。您拥有的一切看起来都是正确的,您是否验证了函数中接收到的内容?请记住,当您的函数返回时,答案可能已过期。文件系统是一个共享资源,其他进程可以随时访问它。Path参数是正确的文件路径吗?这可能是你的问题。你为什么返回字符串?@SLaks我返回字符串是为了测试。稍后我会将函数更改为bool或int。出于某种原因,每当我尝试在路径输入周围添加双引号时,都会失败…看起来您缺少函数的第一行。我更新了代码,请参阅OP,以使用您的函数,但现在我收到两个错误。一个说它不能使用win32exception,我假设我缺少一个正在使用的System.some类,它也可以使用