C# 将文档转换为PDF,错误:";不包含静态';主要';“适用于入口点的方法”;

C# 将文档转换为PDF,错误:";不包含静态';主要';“适用于入口点的方法”;,c#,pdf,doc,C#,Pdf,Doc,我试图用这段代码将文档文件转换成pdf。我得到这个错误“不包含适合入口点的静态“Main”方法” 我不确定这个代码有什么问题。谢谢你的帮助 using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; using System.Text; using Microsoft.Off

我试图用这段代码将文档文件转换成pdf。我得到这个错误“不包含适合入口点的静态“Main”方法” 我不确定这个代码有什么问题。谢谢你的帮助

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Text;
using Microsoft.Office.Interop.Word;

namespace WindowsFormsApplication4
{
public class translate
{
    private static void ConvertWordFileToPdf(string WordFilesLocation, string PdfFilesLocation)
    {
        Document doc = null;
        // C# doesn't have optional arguments so we'll need a dummy value
        object oMissing = System.Reflection.Missing.Value;
        Microsoft.Office.Interop.Word.Application word = null;

        try
        {
            // Create a new Microsoft Word application object
            word = new Microsoft.Office.Interop.Word.Application();

            // Get list of Word files in specified directory
            DirectoryInfo dirInfo = new DirectoryInfo(WordFilesLocation);

            FileInfo[] wordFiles = dirInfo.GetFiles("*.doc");

            if (wordFiles.Length > 0)
            {
                word.Visible = false;
                word.ScreenUpdating = false;
                string sourceFile = "";
                string destinationFile = "";
                try
                {
                    foreach (FileInfo wordFile in wordFiles)
                    {
                        // Cast as Object for word Open method
                        Object filename = (Object)wordFile.FullName;

                        sourceFile = wordFile.Name;
                        destinationFile = "";

                        // Use the dummy value as a placeholder for optional arguments
                        doc = word.Documents.Open(ref filename, ref oMissing,
                            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                            ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                            ref oMissing, ref oMissing, ref oMissing, ref oMissing);
                        doc.Activate();
                        object outputFileName = null;

                        if (wordFile.FullName.ToUpper().Contains(".DOCX"))
                        {
                            outputFileName = wordFile.FullName.Replace(".docx", ".pdf");
                            destinationFile = sourceFile.Replace(".docx", ".pdf");

                        }
                        else
                        {
                            outputFileName = wordFile.FullName.Replace(".doc", ".pdf");
                            destinationFile = sourceFile.Replace(".doc", ".pdf");
                        }

                        sourceFile = WordFilesLocation + "\\" + destinationFile;
                        destinationFile = PdfFilesLocation + "\\" + destinationFile;

                        object fileFormat = WdSaveFormat.wdFormatPDF;

                        // Save document into PDF Format
                        doc.SaveAs(ref outputFileName,
                            ref fileFormat, ref oMissing, ref oMissing,
                            ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                            ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                            ref oMissing, ref oMissing, ref oMissing, ref oMissing);

                        // Close the Word document, but leave the Word application open.
                        // doc has to be cast to type _Document so that it will find the
                        // correct Close method.
                        object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
                        ((_Document)doc).Close(ref saveChanges, ref oMissing, ref oMissing);
                        doc = null;

                        //there is options to save file in particular location, default is the current folder.
                        // So move or replace a file to a new location explicitly
                        if (System.IO.File.Exists(destinationFile))
                        {
                            System.IO.File.Replace(sourceFile, destinationFile, null);
                        }
                        else
                        {
                            System.IO.File.Move(sourceFile, destinationFile);
                        }

                        Console.WriteLine("Success:" + "SourceFile-" + outputFileName.ToString() + " DestinationFile-" + destinationFile);

                    }

                    // word has to be cast to type _Application so that it will find
                    // the correct Quit method.
                    ((_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
                    word = null;
                }
                catch (Exception ex)
                {
                    //individual file exception, do not stop but display the error
                    //Log this if needed
                    Console.WriteLine("Fail:" + "SourceFile-" + sourceFile + "  DestinationFile-" + destinationFile + "#Error-" + ex.Message);
                }
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error occured while processing");
            Console.WriteLine(ex.Message);
        }
        finally
        {
            if (doc != null)
            {
                ((_Document)doc).Close(ref oMissing, ref oMissing, ref oMissing);
                doc = null;

            }
            if (word != null)
            {
                ((_Application)word).Quit(ref oMissing, ref oMissing, ref oMissing);
                word = null;
            }
        }
    }
}

}每个应用程序都需要一个开始执行代码的入口点。C#控制台应用程序寻找名为
Main
的静态函数

因此,假设您正在尝试构建一个控制台应用程序,您将需要添加另一个类,如下所示。(注意:这是在不进行任何验证的情况下从命令行传递前两个参数。)


命名空间Windows窗体应用程序4
{
公共静态类程序
{
公共静态void Main(字符串[]args)
{
translate.ConvertWordFileToPdf(args[0],args[1]);
}
}
}

我同意dabron关于主静态方法的需要

我假设您已将“translate”类添加到Windows窗体项目中,因为您正在使用System.Windows.Forms命名空间

在VS2010上创建Win Forms项目时,您将发现一个名为program.CS的CS文件,该文件应包含如下类:

static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new Form1());
        }
    }
}
静态类程序
{
/// 
///应用程序的主要入口点。
/// 
[状态线程]
静态void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(新Form1());
}
}
}
如果没有,可以创建一个新的Win Forms项目,将类文件添加到该项目中


祝您好运。

您的应用程序中是否只有此代码?从名称空间来看,它听起来像一个WinForm项目。您的应用程序中应该有一个使用此代码的表单。确保项目启动对象设置为使用该表单。右键单击Project并转到properties,在build下您将找到启动项目。Net中的每个可执行文件都需要一个Main方法,如果是WinForm,它将位于program.cs文件中,并且您的Main将调用Form类。我没有考虑名称空间的名称。因此,假设您正在创建一个控制台应用程序可能是不正确的。