Click here to Skip to main content
15,889,034 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hii ,

I want to create one windows application in which i will upload my js files. and they should get converted into minified version of there into some different location.
Posted

Create a Method
C#
static string MiniFyJS(string inputFilePath, string outFilePath)
    {
        string _result = string.Empty;
        if (!string.IsNullOrEmpty(inputFilePath) && !string.IsNullOrEmpty(outFilePath))
        {
            using (var sw = new StreamWriter(outFilePath, false))
            {
                string source = String.Empty;
                try
                {
                    source = File.ReadAllText(inputFilePath);
                }
                catch (IOException)
                {
                    _result = inputFilePath + "File was not found.";
                }
                sw.WriteLine((new Minifier()).MinifyJavaScript(source));
                sw.Flush();
                _result = "Success";
            }
        }
        else
        {
            _result = "Usage: jsmin <file1[,file2,fileN]> <outfile>";
        }
        return _result;
    }

Call this method on button click event
C#
string inputFile = @"D:\JSFiles\calander.js";
       string outFile = @"D:\MinifiedJSFiles\calander.min.js";
       string result = MiniFyJS(inputFile, outFile);
 
Share this answer
 
Comments
Torakami 30-Oct-15 6:40am    
oK , Let me check
Hi, Check this answer, it may be helpful to you,
How to compress Javascript files using C#?
 
Share this answer
 
Comments
Torakami 30-Oct-15 2:14am    
static void Main(string[] args)
{
if (args == null || (args != null && args.Length < 2))
{
Console.WriteLine("Usage: jsmin <file1[,file2,filen]> <outfile>");
return;
}

var argc = args.Length;

if (File.Exists(args[argc - 1]))
{
if (File.GetAttributes(args[argc - 1]) == FileAttributes.ReadOnly)
File.SetAttributes(args[argc - 1], FileAttributes.Normal);
}

using (var sw = new StreamWriter(args[argc - 1]))
{
for (var i = 0; i < argc - 1; ++i)
{
string source = String.Empty;
try
{
source = File.ReadAllText(args[i]);
}
catch (IOException)
{
Console.WriteLine("File \"{0}\" was not found.", args[i]);
continue;
}

sw.WriteLine((new Minifier()).MinifyJavaScript(source));
/* css */
//sw.WriteLine((new Minifier()).MinifyStyleSheet(source, new CssSettings { ColorNames = CssColor.Hex }));
}
sw.Flush();
}
}

THIS IS what they have given , i tried to run this in cosole app , but not sure how args will receive the arguments , it shows 0 length
You have to use AJAXMINIFY dll Microsoft Ajax Minifier[^]
Using above DLL you have call method like
(new Minifier()).MinifyJavaScript(source);

It will return the minified script
You can use it as
C#
static void Main(string[] args)
{
    if (args == null || (args != null && args.Length < 2))
    {
        Console.WriteLine("Usage: jsmin <file1[,file2,fileN]> <outfile>");
        return;
    }
    var argc = args.Length;
    if (File.Exists(args[argc - 1]))
    {
        if (File.GetAttributes(args[argc - 1]) == FileAttributes.ReadOnly)
            File.SetAttributes(args[argc - 1], FileAttributes.Normal);
    }

    using (var sw = new StreamWriter(args[argc - 1]))
    {
        for (var i = 0; i < argc - 1; ++i)
        {
            string source = String.Empty;
            try
            {
                source = File.ReadAllText(args[i]);
            }
            catch (IOException)
            {
                Console.WriteLine("File \"{0}\" was not found.", args[i]);
                continue;
            }
            sw.WriteLine((new Minifier()).MinifyJavaScript(source));      
        }
        sw.Flush();
    }
}

Syntax to pass parameter
jsmin <file1[,file2,fileN]> <outfile>

Pass command line argument like
C#
jsmin jquery-1.4.4.js jquery.min.js
 
Share this answer
 
Comments
Torakami 30-Oct-15 2:41am    
this is the code i have tried in console application by adding dll , but then when application strats at main , in in first line args lengths 0 , then it simply prints the first line on console and returns ,, thats it
[no name] 30-Oct-15 2:46am    
you have to call this console application exe from Commandline .
Like if your application is ConsoleApplication1 then
in command prompt write
[Full Path to your exe] jsmin [input File Path with FileName] [Output File Path with FileName]
Torakami 30-Oct-15 5:28am    
NO , THATS OK , MY QUESTION IS CAN I GENERATE ALL THIS THING IN CODE ONLY ,, DONT WANT TO USE COMMAND .. I WANT TO HANDLE FROM DODE ONLY
[no name] 30-Oct-15 5:48am    
Then you have to change the implementation. Replace main method with a customized method which will take inputFilePath, OutputFilePath. Make a Windows form with two textboxes for inputFilePath and outputFilePath and one button . Call the customized method on button click. passtextboxes value in that method.
Torakami 30-Oct-15 5:59am    
OK , THIS IS WHAT I HAVE TRIED , CAN YOU PLEASE HELP ME ...

static void Main(string[] args)
{
string[] arr = {"~/Sourse/CustomerLocation.js"};
MinimizeJsFiles(arr);
}

private static void MinimizeJsFiles(string[] args)
{
if (args == null || (args != null && args.Length < 2))
{
Console.WriteLine("Usage: jsmin ~/Sourse/CustomerLocation.js jquery.min.js");
return;
}

var argc = args.Length;

if (File.Exists(args[argc - 1]))
{
if (File.GetAttributes(args[argc - 1]) == FileAttributes.ReadOnly)
File.SetAttributes(args[argc - 1], FileAttributes.Normal);
}

using (var sw = new StreamWriter(args[argc - 1]))
{
for (var i = 0; i < argc - 1; ++i)
{
string source = String.Empty;
try
{
source = File.ReadAllText(args[i]);
}
catch (IOException)
{
Console.WriteLine("File \"{0}\" was not found.", args[i]);
continue;
}

sw.WriteLine((new Minifier()).MinifyJavaScript(source));
/* css */
//sw.WriteLine((new Minifier()).MinifyStyleSheet(source, new CssSettings { ColorNames = CssColor.Hex }));
}
sw.Flush();
}
}

i JUST WANTED TO GENERATE MIN VERSION ON SEPERATE LOCATIONS

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900