Posts: 1,187
Threads: 276
Joined: Jul 2022
08-03-2025, 03:40 AM
(This post was last modified: 08-03-2025, 03:41 AM by Davider.)
The following two scripts do not throw any exceptions when executed, but they do not generate a DOCX file. Why?
// script "csRtCo_spire.doc.cs"
/*/ role exeProgram; outputPath %folders.Workspace%\exe\csRtCo_spire.doc; c CsScript.cs; /*/ //.
script.setup(trayIcon: true, sleepExit: true);
//..
print.clear();
string code = """
using Spire.Doc;
using System;
string sDir = @"C:\Users\Administrator\Desktop";
string sTxt = "hello world!";
string sName = "hello.docx";
newDoc(sDir, sTxt, sName);
void newDoc(string sDir, string sTxt, string sName)
{
Environment.CurrentDirectory = sDir;
var doc = new Document();
var section = doc.AddSection();
var paragraph = section.AddParagraph();
paragraph.AppendText(sTxt);
doc.SaveToFile(sName, FileFormat.Docx2016);
}
""";
var c = CsScript.Compile(code);
if (c == null) return;
c.Run();
// script "csRtCo_spire.doc2.cs"
/*/ role exeProgram; outputPath %folders.Workspace%\exe\csRtCo_spire.doc2; c CsScript.cs; /*/ //.
script.setup(trayIcon: true, sleepExit: true);
//..
print.clear();
string code = """
using Spire.Doc;
using System;
string sDir = @"C:\Users\Administrator\Desktop";
string sTxt = "hello world!";
string sName = "hello.docx";
newDoc(sDir, sTxt, sName);
void newDoc(string sDir, string sTxt, string sName)
{
Environment.CurrentDirectory = sDir;
var doc = new Document();
var section = doc.AddSection();
var paragraph = section.AddParagraph();
paragraph.AppendText(sTxt);
doc.SaveToFile(sName, FileFormat.Docx2016);
}
""";
var errors = new List<string>();
var c = CsScript.Compile(code, r: new[] { @"D:\RPA\spire\10.5.0\NET9.0\Spire.Doc.dll" }, errors: errors);
if (c == null) {
Console.WriteLine("Failed to compile:");
foreach (var error in errors) {
Console.WriteLine(error);
}
return;
}
c.Run();
Posts: 12,232
Threads: 144
Joined: Dec 2002
08-03-2025, 03:49 AM
(This post was last modified: 08-03-2025, 03:51 AM by Gintaras.)
Prints error in LA Output:
Quote:FAILED TO COMPILE
code(1,7): error CS0246: The type or namespace name 'Spire' could not be found (are you missing a using directive or an assembly reference?)
code(12,19): error CS0246: The type or namespace name 'Document' could not be found (are you missing a using directive or an assembly reference?)
code(16,27): error CS0103: The name 'FileFormat' does not exist in the current context
Quote:Failed to compile:
Failed to load r. System.IO.FileNotFoundException: Could not find a part of the path 'D:\RPA\spire\10.5.0\NET9.0\Spire.Doc.dll'.
File name: 'D:\RPA\spire\10.5.0\NET9.0\Spire.Doc.dll'
---> System.IO.DirectoryNotFoundException: Could not find a part of the path 'D:\RPA\spire\10.5.0\NET9.0\Spire.Doc.dll'.
at line 41 in CsScript.cs, CsScript.<>c.<Compile>b__3_1(String o)
at line 41 in CsScript.cs, CsScript.Compile(String code, Boolean library, String[] c, String[] r, List`1 errors)
>>
--- Raw stack trace ---
at Microsoft.Win32.SafeHandles.SafeFileHandle.CreateFile(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options)
at Microsoft.Win32.SafeHandles.SafeFileHandle.Open(String fullPath, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
at System.IO.Strategies.OSFileStreamStrategy..ctor(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
at System.IO.Strategies.FileStreamHelpers.ChooseStrategyCore(String path, FileMode mode, FileAccess access, FileShare share, FileOptions options, Int64 preallocationSize, Nullable`1 unixCreateMode)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
at Roslyn.Utilities.StandardFileSystem.OpenFile(String filePath, FileMode mode, FileAccess access, FileShare share) in C:\code-lib\roslyn\src\Compilers\Core\Portable\FileSystem\ICommonCompilerFileSystem.cs:line 64
at Roslyn.Utilities.CommonCompilerFileSystemExtensions.OpenFileWithNormalizedException(ICommonCompilerFileSystem fileSystem, String filePath, FileMode fileMode, FileAccess fileAccess, FileShare fileShare) in C:\code-lib\roslyn\src\Compilers\Core\Portable\FileSystem\ICommonCompilerFileSystem.cs:line 32
at Roslyn.Utilities.CommonCompilerFileSystemExtensions.OpenFileWithNormalizedException(ICommonCompilerFileSystem fileSystem, String filePath, FileMode fileMode, FileAccess fileAccess, FileShare fileShare) in C:\code-lib\roslyn\src\Compilers\Core\Portable\FileSystem\ICommonCompilerFileSystem.cs:line 40
at Microsoft.CodeAnalysis.MetadataReference.CreateFromFile(String path, MetadataReferenceProperties properties, DocumentationProvider documentation) in C:\code-lib\roslyn\src\Compilers\Core\Portable\MetadataReference\MetadataReference.cs:line 245
at CsScript.<>c.<Compile>b__3_1(String o) in C:\code\ok\files\Extensions\CsScript\CsScript.cs:line 41
at System.Linq.Enumerable.ArraySelectIterator`2.MoveNext()
at System.Collections.Generic.SegmentedArrayBuilder`1.AddNonICollectionRange(IEnumerable`1 source)
at System.Linq.Enumerable.Concat2Iterator`1.ToArray()
at CsScript.Compile(String code, Boolean library, String[] c, String[] r, List`1 errors) in C:\code\ok\files\Extensions\CsScript\CsScript.cs:line 41
Posts: 1,187
Threads: 276
Joined: Jul 2022
Posts: 12,232
Threads: 144
Joined: Dec 2002
Try to click the Debug button instead of Run.
Posts: 1,187
Threads: 276
Joined: Jul 2022
output:
Failed to attach debugger.
Posts: 12,232
Threads: 144
Joined: Dec 2002
Try without role exeProgram.
Remove `print.clear();`.
Does it mean that the LA debugger does not work with any script?
Posts: 1,187
Threads: 276
Joined: Jul 2022
08-03-2025, 04:50 AM
(This post was last modified: 08-03-2025, 04:51 AM by Davider.)
Quote:Try without role exeProgram.
By using the following role, you can display compilation error messages in the output panel.
/*/ c CsScript.cs; /*/ //.
Quote:Does it mean that the LA debugger does not work with any script?
I rarely use the LA debugger, so I'm not very familiar with it.
Posts: 12,232
Threads: 144
Joined: Dec 2002
Does the exe program exist? Maybe removed by an antivirus.
Posts: 1,187
Threads: 276
Joined: Jul 2022
08-03-2025, 05:26 AM
(This post was last modified: 08-03-2025, 05:47 AM by Davider.)
Quote:Does the exe program exist?
exist!
After many attempts, I found that:
In the `csRtCo_spire.doc2` code, the exe role code must be removed in order to successfully output a DOCX file.
It is not possible to create a DOCX file using the generated EXE program.
Posts: 12,232
Threads: 144
Joined: Dec 2002
If can't attach debugger, try to insert `print.it` in various places to find the place where it fails. Before CsScript.Compile and in CsScript.Compile. And replace `Console.WriteLine` with `print.it`.
Posts: 1,187
Threads: 276
Joined: Jul 2022
Posts: 1,187
Threads: 276
Joined: Jul 2022
08-06-2025, 11:46 PM
(This post was last modified: 08-06-2025, 11:47 PM by Davider.)
var c = CsScript.Compile(code, r: new[] { @"D:\RPA\spire\10.6.0\NET9.0\Spire.Doc.dll" }); //before
Because the code I need to compile always uses Spire.Doc.dll, so I modified the above line as follows, and moving the path of Spire.Doc.dll to the first line of the code.
However, after execution, it still reports that the DLL cannot be loaded. How should I resolve this?
var c = CsScript.Compile(code); //after
// script "cs_spire.doc.cs"
/*/ r D:\RPA\spire\10.6.0\NET9.0\Spire.Doc.dll; c CsScript.cs; /*/ //.
script.setup(trayIcon: true, sleepExit: true);
//..
print.clear();
string code = """
using Spire.Doc;
using Spire.Doc.License;
using System;
string sDir = @"C:\Users\Administrator\Desktop";
string sTxt = "hello world!";
string sName = "hello.docx";
newDoc(sDir, sTxt, sName);
void newDoc(string sDir, string sTxt, string sName)
{
Environment.CurrentDirectory = sDir;
var doc = new Document();
var section = doc.AddSection();
var paragraph = section.AddParagraph();
paragraph.AppendText(sTxt);
doc.SaveToFile(sName, FileFormat.Docx2016);
}
""";
//var c = CsScript.Compile(code, r: new[] { @"D:\RPA\spire\10.6.0\NET9.0\Spire.Doc.dll" }); //before
var c = CsScript.Compile(code); //after
if (c == null) return;
c.Run();
Posts: 12,232
Threads: 144
Joined: Dec 2002
Works here. Maybe not all Spire dlls are in your dll folder + runtimes subfolder.
// script "csRtCo_spire.doc.cs"
/*/ role exeProgram; outputPath %folders.Workspace%\exe\csRtCo_spire.doc; c CsScript.cs; /*/
/*/ c CsScript.cs; /*/
//print.clear();
//print.redirectConsoleOutput = true; //if using Console.WriteLine
string code = """
using Spire.Doc;
using System;
Au.print.it("C# script started");
string sDir = @"C:\Test\spire";
string sTxt = "hello world!";
string sName = "hello.docx";
newDoc(sDir, sTxt, sName);
void newDoc(string sDir, string sTxt, string sName)
{
Environment.CurrentDirectory = sDir;
var doc = new Document();
var section = doc.AddSection();
var paragraph = section.AddParagraph();
paragraph.AppendText(sTxt);
doc.SaveToFile(sName, FileFormat.Docx2016);
}
""";
var c = CsScript.Compile(code, r: [@"C:\code\ok\.nuget\spire\Spire.Doc.dll"]);
//var c = CsScript.Compile(code, r: [folders.ThisApp + "Spire.Doc.dll"]); //if spire dlls are in the exe folder
if (c == null) return;
c.Run();
Posts: 1,187
Threads: 276
Joined: Jul 2022
Thanks for your testing.
Since all of my runtime-compiled code is related to Spire.Doc.dll, I’d like to simplify the code as much as possible.
Is it possible to compile and execute successfully using only the code below? How can I achieve that?
I removed the `using` directives and DLL references. I want to make these functionalities available globally.
__________________________________________________________________________________
string code = """
string sDir = @"C:\Test\spire";
string sTxt = "hello world!";
string sName = "hello.docx";
newDoc(sDir, sTxt, sName);
void newDoc(string sDir, string sTxt, string sName)
{
Environment.CurrentDirectory = sDir;
var doc = new Document();
var section = doc.AddSection();
var paragraph = section.AddParagraph();
paragraph.AppendText(sTxt);
doc.SaveToFile(sName, FileFormat.Docx2016);
}
""";
var c = CsScript.Compile(code);
c.Run();
|