Skip to main content
 首页 » 编程设计

c#之将此 C# 代码转换为经典 ASP/VBScript

2024年02月24日22jyk

我知道这听起来倒退,但我必须将其从 C# 转换为经典 ASP。我不懂任何 VBScript,所以我需要帮助。

在我的 C# 代码中,它读取配置文件中的 appkeys,解析它们,并使用循环来执行一个过程。我不知道如何在 VBScript 中执行字典和 I/O 操作。有人可以帮忙吗?

这些是我想我必须作为常量变量存储在 .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" /> 

这是 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" 
                } 
            } 
        } 

更新: 按照下面的建议,我转换为 VB 以便于翻译。看来我也得重新考虑一下了。这是我所在的位置,但出现错误:

<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> 

这是我遇到的错误:

Microsoft VBScript compilation error '800a03ea'  
 
Syntax error  
 
/sandbox/aspjpeg/Default.asp, line 45  
 
resizeValues = Dictionary(String, Integer)() 
----------------------------------^ 

请您参考如下方法:

您需要重新考虑您正在做的很多事情。 .Net 不会逐行翻译成 vbscript,甚至不会逐个类/模块翻译成 vbscript。您必须查看 ASP.Net 实际在做什么,并弄清楚如何在 vbscript 中执行此操作。

vbscript 的一个技巧是,与其乱用 vbscript 文件 io 来处理配置文件,我发现通过在单独的文件中定义这些变量来处理配置数据要容易得多,然后将这些变量包含在其他地方。然后您可以像使用其他变量一样使用该数据。