in

CodeSmith Community

Your Code. Your Way. Faster!

UnauthorizedAccessException when running a template with .. or environment variable

Last post 05-05-2008 9:24 AM by blake05. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 05-05-2008 5:05 AM

    • shovavnik
    • Not Ranked
    • Joined on 08-19-2007
    • Posts 4
    • Points 110

    UnauthorizedAccessException when running a template with .. or environment variable

    I'm using templates on a number of machines that all have slightly different folder layouts. For example, on one machine the projects folder is in C:\Projects and on another it's at D:\Projects. So I created an environment variable %PROJECTS% that maps correctly to the folder and used that variable in the paths in CS templates.

    Now when I run the templates, I get a UnauthorizedAccessException. If I use the full path, it runs correctly, so it's not a permission issue.

    I get the same exception when I use .. in the path. In previous versions (I upgraded to 4.1.4 yesterday), .. would travel up a directory from the directory from which the csp file was run, but now it seems to use the system32 directory.

    Is there any way to use a non-absolute path? Is there any way to work around these issues? I couldn't find anything relevant in the forums or elsewhere, but if I missed something, just let me know.

    Thanks in advance!

    • Post Points: 35
  • 05-05-2008 9:24 AM In reply to

    • blake05
    • Top 25 Contributor
    • Joined on 04-03-2008
    • Wisconsin
    • Posts 166
    • Points 2,790

    Re: UnauthorizedAccessException when running a template with .. or environment variable

    Answer

    Hello,

    Here is a quick work around. You will need to modify your template a little bit but the following sample template will resolve an environment variable. I'll also submit a bug report :).

    <%@ CodeTemplate Language="C#" TargetLanguage="Text" src="" Inherits="" Debug="False" Description="Template description here." %>
    <%@ Assembly Name="System.Data" %>
    <%@ Import Namespace="System.Data" %>

    environment Variable (%userprofile%): <%= ResolveEnvironmentVariablePath("%userprofile%") %>

    <script runat="template">

    public string ResolveEnvironmentVariablePath(string environmentVariable)
    {
        //http://msdn.microsoft.com/en-us/library/system.environment.expandenvironmentvariables.aspx
        return System.Environment.ExpandEnvironmentVariables(environmentVariable);
    }

    </script>

    Thanks

    -Blake Niemyjski

    Blake Niemyjski
    CodeSmith Tools, LLC
    Support Specialist
    • Post Points: 5
Page 1 of 1 (2 items)
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems