简体   繁体   English

将此C#代码转换为Classic ASP / VBScript?

[英]Convert this C# code to Classic ASP / VBScript?

I know this sounds backwards, but I have to convert this from C# to Classic ASP. 我知道这听起来倒退,但我必须将其从C#转换为Classic ASP。 I don't know any VBScript so I need help. 我不知道任何VBScript,所以我需要帮助。

In my C# code, it is reading appkeys in the config file, parsing them, and using loops to perform a process. 在我的C#代码中,它正在读取配置文件中的appkeys,解析它们,并使用循环来执行一个过程。 I don't know how to do dictionaries and I/O stuff in VBScript. 我不知道如何在VBScript中做字典和I / O的东西。 Can someone please help with this? 有人可以帮忙吗?

These are the keys which I guess I would have to store as constant variables in the .asp file: 这些是我想我必须在.asp文件中存储为常量变量的键:

<add key="Output.Size" value="550" />
<add key="Output.Ext" value=".jpg" />
<add key="Output.Folder" value="thumbs" />
<add key="Suffix.LG" value="750" />
<add key="Suffix.TN" value="250" />
<add key="Suffix.TNL" value="175" />
<add key="Suffix.TNR" value="75" />
<add key="Supported" value=".jpeg,.jpg,.gif,.bmp,.tiff,.png" />

This is the C# code: 这是C#代码:

Generate generate = new Generate();
generate.Process(source, destination); //inputs will be relative URL paths

    public class Generate
    {
        private const string OUTPUT_SIZE_KEY = "Output.Size";
        private const string OUTPUT_EXT_KEY = "Output.Ext";
        private const string SUFFIX_KEY = "Suffix.";
        private const string SUPPORTED_KEY = "Supported";
        private string[] supportedExt = null;

        public Generate()
        {
            //GET ALL SUPPORTED FORMAT TYPES TO PREVENT PROCESSING ON UNSUPPORTED FILES
            supportedExt = ConfigurationManager.AppSettings[SUPPORTED_KEY].ToLower().Split(',');
        }

        public void Process(string sourceDir, string destDir)
        {
            int thumbSize = Int32.Parse(ConfigurationManager.AppSettings[OUTPUT_SIZE_KEY]);
            string thumbExt = ConfigurationManager.AppSettings[OUTPUT_EXT_KEY];

            //COLLECT VALUES FOR RESIZING
            Dictionary<string, int> resizeValues = new Dictionary<string, int>();
            foreach (string item in ConfigurationManager.AppSettings.AllKeys)
            {
                if (item.StartsWith(SUFFIX_KEY))
                {
                    resizeValues.Add(item.Substring(SUFFIX_KEY.Length), Int32.Parse(ConfigurationManager.AppSettings[item]));
                }
            }

            //BEGIN GENERATING THUMBS
            foreach (string item in Directory.GetFiles(sourceDir))
            {
                //VALIDATE IF FILE TYPE SUPPORTED
                if (!supportedExt.Contains(Path.GetExtension(item.ToLower())))
                    continue;

                string fileName = Path.GetFileNameWithoutExtension(item);
                string outputFile = Path.Combine(destDir, fileName + thumbExt);

                //RESIZE TO THUMB
                Resize(item, outputFile, thumbSize); //DO NOT HAVE TO CONVERT "RESIZE"

                //RESIZE TO DIFFERENT THUMBS
                foreach (KeyValuePair<string, int> output in resizeValues)
                {
                    string thumbSeq = Path.Combine(destDir, fileName + output.Key + thumbExt);
                    Resize(item, thumbSeq, output.Value); //DO NOT HAVE TO CONVERT "RESIZE"
                }
            }
        }

UPDATE: As suggested below, I converted to a VB for easier translation. 更新:如下所示,我转换为VB以便于翻译。 It seems I have to rethink things as well. 我似乎也必须重新考虑事情。 This is where I am but am getting an error: 这是我的地方,但我收到一个错误:

<html>
<head>
    <title></title>
</head>
<body>
    <% 

    'DECLARE VARIABLES
Dim outputSize
Dim outputExt
Dim outputSuffix()
Dim supported
Dim source
Dim destination

'INITIALIZE VALUES
outputSize = 550
outputExt = ".jpg"
outputSuffix(0) = "LG.750"
outputSuffix(1) = "TN.250"
outputSuffix(2) = "TNL.175"
outputSuffix(3) = "TNR.75"
supported = ".jpeg,.jpg,.gif,.bmp,.tiff,.png"
source = "catalog/upload"
destination = "catalog"

'CALL FUNCTION TO RESIZE THUMBNAILS
Dim generate
generate = New ThumbGenerator
generate.Process source, destination

'PROCESS TO RESIZE
class ThumbGenerator
    Dim supportedExt

    Public Sub Process(sourceDir, destDir)
        Dim thumbSize
        Dim thumbExt
        thumbSize = outputSize
        thumbExt = outputExt
        supportedExt = supported.ToLower().Split(",")

        'COLLECT VALUES FOR RESIZING
        Dim resizeValues
        resizeValues = Dictionary(String, Integer)()
        For Each item As String In outputSuffix
            Dim temp
            temp = item.Split(".")
            resizeValues.Add(temp(0), temp(1))
        Next

        'BEGIN GENERATING THUMBS
        For Each item As String In Directory.GetFiles(sourceDir)
            'VALIDATE IF FILE TYPE SUPPORTED
            If Not supportedExt.Contains(Path.GetExtension(item.ToLower())) Then
                Continue For
            End If

            Dim fileName
            Dim outputFile
            fileName = Path.GetFileNameWithoutExtension(item)
            outputFile = Path.Combine(destDir, fileName + thumbExt)

            'RESIZE TO THUMB
            'Resize(item, outputFile, thumbSize)

            'RESIZE TO DIFFERENT THUMBS
            For Each output As KeyValuePair(Of String, Integer) In resizeValues
                Dim thumbSeq As String = Path.Combine(destDir, fileName + output.Key + thumbExt)
                'Resize(item, thumbSeq, output.Value)
            Next
        Next
    End Sub
End Class
    %>
</body>
</html>

This is the error I am getting: 这是我得到的错误:

Microsoft VBScript compilation error '800a03ea' 

Syntax error 

/sandbox/aspjpeg/Default.asp, line 45 

resizeValues = Dictionary(String, Integer)()
----------------------------------^

You'll want to rethink a lot of what you're doing. 你想要重新思考你正在做的很多事情。 The .Net won't translate to vbscript line by line, or even class/module by class/module. .Net不会逐行转换为vbscript,甚至不会按类/模块转换为类/模块。 You'll have to look at what the ASP.Net is actually doing and figure out how to do that in vbscript. 你将不得不看看ASP.Net实际上在做什么,并弄清楚如何在vbscript中做到这一点。

One tip for vbscript is that rather than mess with vbscript file io to handle the config file, I found it much easier to handle configuation data by defining those variables in a separate file that will then be included in other other places. vbscript的一个提示是,不是乱用vbscript文件来处理配置文件,我发现通过在单独的文件中定义这些变量来处理配置数据要容易得多,然后将其包含在其他地方。 Then you can use that data just like you would other variables. 然后您可以像使用其他变量一样使用该数据。

You can not Dim a variable and then set the value to it on the same line in vbscript. 您不能对变量进行Dim,然后在vbscript的同一行上将值设置为它。

Dim outputSize As Integer outputSize = 550 Dim outputSize As Integer outputSize = 550

And don't use "" around an integer. 并且不要在整数周围使用“”。

Use Const keyword to define constant variables. 使用Const关键字定义常量变量。 for example: 例如:

Dim outputSize, outputExt
outputSize = 550
outputExt = ".jpg"

can be

Const outputSize = 550, outputExt = ".jpg"

Use FSO object to handle files and folders. 使用FSO对象来处理文件和文件夹。 for example: 例如:

    Set fso = Server.CreateObject("Scripting.FileSystemObject")
    Set folder = fso.GetFolder(sourceDir)
    Set files = folder.Files

(I won't convert it for you but...) (我不会为你换货,但......)

There exist several C# <-> VB.NET converters, eg http://converter.telerik.com/ 有几个C#< - > VB.NET转换器,例如http://converter.telerik.com/

Once you have converted this to VB.NET it might be little bit easier to convert it to classic ASP / VBScript. 一旦将其转换为VB.NET,将其转换为经典ASP / VBScript可能会更容易一些。

you CAN dim a variable and assign a valuy by 你可以调暗变量并赋值

dim myVar : myVar = 4711

or 要么

' this will create a "dictionary"

dim resizeValues : set resizeValues = server.createobject("scripting.dictionary")

Does it need to live in a bygone era? 它需要生活在过去的时代吗? Or merely be called from a bygone era? 或者只是从过去的时代开始? If the latter is the answer, have you considered moving the code to its own assembly and making the assembly COM visible then just accessing it from said bygone era. 如果后者是答案,您是否考虑将代码移动到自己的程序集并使程序集COM可见,然后从过去的时代访问它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM