开发者

User Permissions issue

开发者 https://www.devze.com 2023-02-27 14:36 出处:网络
I am using Visual Studio C# to parse an XML document for a file location from a local search tool I am using. Specifically I am using c# to query if the user has access to certain files and hide those

I am using Visual Studio C# to parse an XML document for a file location from a local search tool I am using. Specifically I am using c# to query if the user has access to certain files and hide those to which it does not have access. I seem to have fi开发者_如何学Cles that should return access is true however because not all files are local (IE some are web files without proper names) it is not showing access to files it should be showing access to. The error right now is caused by a url using .aspx?i=573, is there a work around or am I going to have to just remove all of these files... =/

Edit: More info...

I am using right now....

foreach (XmlNode xn in nodeList)
            {

                string url = xn.InnerText;
                //Label1.Text = url;
                try
                { using (FileStream fs = File.OpenRead(url)) {  }
                }
                catch { i++; Label2.Text = i.ToString(); Label1.Text = url; }


            }

The issue is, when it attempts to open files like the ....aspx?i=573 it puts them in the catch stack. If I attempt to open the file however the file opens just fine. (IE I have read access but because of either the file type or the append of the '?=' in the file name it tosses it into the unreadable stack.

I want everything that is readable either via url or local access to display else it will catch the error files for me.


I'm not sure exactly what you are trying to do, but if you only want the path of a URI, you can easily drop the query string portion like this:

        Uri baseUri = new Uri("http://www.domain.com/");
        Uri myUri = new Uri(baseUri, "home/default.aspx?i=573");

        Console.WriteLine(myUri.AbsolutePath); // ie "home/default.aspx"

You cannot have ? in file names in Windows, but they are valid in URIs (that is why IE can open it, but Windows cannot).

Alternatively, you could just replace the '?' with some other character if you are converting a URL to a filename.

In fact thinking about it now, you could just check to see if your "document" was a URI or not, and if it isn't then try to open the file on the file system. Sounds like you are trying to open any and everything that is supplied, but it wouldn't hurt to performs some checks on the data.

private static bool IsLocalPath(string p)
{
  return new Uri(p).IsFile;
}

This is from Check if the path input is URL or Local File it looks like exactly what you are looking for.


FileStream reads and writes local files. "?" is not valid character for local file name.

It looks like you want to open local and remote files. If it is what you are trying to do you should use approapriate metod of downloading for each type - i.e. for HTTP you WebRequest or related classes.

Note: it would be much easier to answer if you'd say: when url is "..." File.OpenRead(url) failes with exception, mesasge "...".

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号