File path issue in mac - .net-core

nopCommerce version: 4.1
I am using nopCommerce version 4.1 on mac os with visual studio for mac, when I run this solution, I get error on creating path.
Plugin 'Must be assigned to customer role'. Could not load file or
assembly
'/Users/alireza/Desktop/NopCommerce/Presentation/Nop.Web/Plugins\bin/Nop.Plugin.DiscountRules.CustomerRoles.dll'.
The system cannot find the file specified.\n\nCould not load file or
assembly
'/Users/alireza/Desktop/NopCommerce/Presentation/Nop.Web/Plugins\bin/Nop.Plugin.DiscountRules.CustomerRoles.dll'.
The system cannot find the file specified.\n\n

I found the solution by myself:
path = Environment.OSVersion.Platform == PlatformID.Win32NT
? path.Replace("~/", string.Empty).TrimStart('/').Replace('/', '\\')
: path.Replace("~/", string.Empty).TrimStart('/');

in the file opCommerce_4.10_Source/Libraries/Nop.Core/Infrastructure/NopFileProvider.cs
replace the the following function
public virtual string MapPath(string path)
with the following code :
public virtual string MapPath(string path)
{
// path = path.Replace("~/", string.Empty).TrimStart('/').Replace('/', '\\');
path = Environment.OSVersion.Platform == PlatformID.Win32NT
? path.Replace("~/", string.Empty).TrimStart('/').Replace('/', '\\')
: path.Replace("~/", string.Empty).TrimStart('/');
return Path.Combine(BaseDirectory ?? string.Empty, path);
}

Related

How to get the root dir in Windows and Linux?

I wrote a small software using .net6 which should run on Windows and Linux (Ubuntu). In this software I need to access a file in a folder.
Linux: /folder1/folder2/file.txt
Windows: d:\folder1\folder2\file.txt
The folder structure and the filename is the same on both systems.
This code works so far
string[] pfad;
pfad = new[] { "folder1", "folder2","file.txt" };
Console.WriteLine(System.IO.Path.Combine(pfad));
and delivers the correct folder structur under Linux and Windows.
How can I define the root directory?
/ in Linux and d:\ in Windows
Can I detect the OS type somehow or what is the best approach?
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); is "fix" under Windows to C:... - I want to use another drive.
Borrowing from stefan answer but using OperatingSystem class instead of RuntimeInformation (since OperatingSystem is part of System i believe it's preferable)
string rootPath;
if (OperatingSystem.IsWindows())
rootPath = #"d:\";
else if (OperatingSystem.IsLinux())
rootPath = "/";
else
{
// maybe throw an exception
}
You can use System.Runtime.InteropServices.RuntimeInformation like this:
string rootPath;
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
rootPath = #"d:\";
}
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
{
rootPath = "/";
}

EFCore SQLite connection string with relative path in asp.net

I have just added SQLite to my asp.net webApi project, and am having trouble working out how get the path to the App_Data folder to pass to DbContextOptionsBuilderUseSqlite
I have the following in the web.config I have a link to an external a config file with the conenction string...
<connectionStrings configSource="config\connectionStrings.config"/>
and in there I have...
<connectionStrings>
<add name="MyDatastore"
connectionString="DataSource=./App_Data/test.sqlite" />
</connectionStrings>
And in my DbContext.OnConfiguring I Have....
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
string path = WebConfigurationManager.ConnectionStrings["MyDatastore"].ConnectionString;
optionsBuilder.UseSqlite(path);
}
}
The path is correctly retrieved (I can see I get the path as configured on connectionStrings.config
so ./App_Data/test.sqlite is passed to optionsBuilder.UseSqlite(path).
However, I get the following error...
SQLite Error 14: 'unable to open database file'.
If I use just connectionString="DataSource=test.sqlite" /> then it seems to magically find the file in the App_Data folder, when I ran on my dev machine in debug, but I had problems on another machine (release build). I assume it is the path, though all I get back is 'unable to open database file'.
I also tried..
connectionString="DataSource=|DataDirectory|test.sqlite" />
This gives me a Illegal characters in path error.
The following does work (full path)
connectionString="d:\0\test.sqlite" />
But I want to be able to use relative paths, eg maybe even .\datastore\test.sqlite.
Does any one have any ideas on this?
Thanks in advance
You'll have to fix up the relative paths at runtime:
var builder = new SqliteConnectionStringBuilder(connectionString);
builder.DataSource = Path.GetFullPath(
Path.Combine(
AppDomain.CurrentDomain.GetData("DataDirectory") as string
?? AppDomain.CurrentDomain.BaseDirectory,
builder.DataSource);
connectionString = builder.ToString();
Works perfectly for me.
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
var dataSource = Path.Combine(System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "siteDB.db");
optionsBuilder
.UseSqlite($"Data Source={dataSource};");
}
Note: This solution was tested for .Net Core 5, and one can presume it will work on 2.x, 3.x, 5
If you want to use a diferent project than the one provided when you started, you have to specify the correct path ("Data Source = ..\\MyApplication.DAL\\sqliteDatabase.db") in the appsettings.json.
In this presented case, you don't even need to write the method OnConfiguring(DbContextOptionsBuilder optionsBuilder) in the ApplicationDbContext.cs.
You have a full setup bellow (Startup & appsettings.json).
My project structure:
-> MyApplication (solution)
-> MyApplication.UI (initial project of the solution)
-> MyApplication.BL (project)
-> MyApplication.DAL (project)
Inside Startup.cs
public void ConfigureServices(IServiceCollection services)
{
//... other services
services.AddDbContext<ApplicationDbContext>
(x => x.UseSqlite(Configuration.GetConnectionString("SqliteConnection")));
//.... other services and logic
}
In appsettings.json :
"ConnectionStrings": {
"SqliteConnection": "Data Source = ..\\MyApplication.DAL\\sqliteDatabase.db"
}
Works for me on linux, .net core 5.
var builder = new SqliteConnectionStringBuilder("Data Source=MyDatabase.db");
builder.DataSource = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, builder.DataSource);
services.AddDbContext<MyContext>(o => o.UseSqlite(builder.ToString());
Assumes database is in the bin directory, e.g. MyProject/bin/Debug/MyDatabase.db or MyProject/bin/Release/MyDatabase.db.
If you are a .Net Core backend developer who use sqlite, make sure to use below code example. Otherwise SQLite Error 14: 'unable to open database file' error will come.
Startup.cs
var baseDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
string dbPathSystemLog = Path.Combine(baseDirectory, "CAMSCoreSystemLog.db");
SystemLogDBContext.cs
public class SystemLogDBContext : DbContext
{
public SystemLogDBContext(DbContextOptions<SystemLogDBContext> options) : base(options)
{
Database.EnsureCreated();
}
}
This line will create the Db if not exist
Database.EnsureCreated();
I was struggling two days. This will help someone.

Unable to create folder with RCurl

I'm having trouble using the ftpUpload() function of RCurl to upload a file to a non-existent folder in an SFTP. I want the folder to be made if its not there, using the ftp.create.missing.dirs option. Here's my code currently:
.opts <- list(ftp.create.missing.dirs=TRUE)
ftpUpload(what = "test.txt",
to "sftp://ftp.testserver.com:22/newFolder/existingfile.txt",
userpwd = paste(user, pwd, sep = ":"), .opts = opts)`
It doesn't seem to be working as I get the following error:
* Initialized password authentication
* Authentication complete
* Failed to close libssh2 file
I can upload a file to an existent folder with success, its just when the folder isn't there I get the error.
The problem seems be due the fact you are trying to create the new folder, as seen in this question: Create an remote directory using SFTP / RCurl
The error can be found in Microsoft R Open git page:
case SSH_SFTP_CLOSE:
if(sshc->sftp_handle) {
rc = libssh2_sftp_close(sshc->sftp_handle);
if(rc == LIBSSH2_ERROR_EAGAIN) {
break;
}
else if(rc < 0) {
infof(data, "Failed to close libssh2 file\n");
}
sshc->sftp_handle = NULL;
}
if(sftp_scp)
Curl_safefree(sftp_scp->path);
In the code the parameter rc is related to libssh2_sftp_close function (more info here https://www.libssh2.org/libssh2_sftp_close_handle.html), that tries close the nonexistent directory, resulting in the error.
Try use curlPerform as:
curlPerform(url="ftp.xxx.xxx.xxx.xxx/";, postquote="MkDir /newFolder/", userpwd="user:pass")

tess4j The specified module could not be found

please need your help.. I testing Tess4j but i have a problem
Development environment : eclipse, jdk 1.7
Code
System.setProperty("jna.library.path", "32".equals(System.getProperty("sun.arch.data.model")) ? "/lib/win32-x86" : "/lib/win32-x86-64");
File imgFile = new File("d:\\testImg\\testimg2-1.png");
System.setProperty("jna.debug_load", "true");
Tesseract prc = new Tesseract();
prc.setDatapath("/tessdata");
try{
String rslt = prc.doOCR(imgFile);
}catch(Exception e){
e.printStackTrace();
}
Error
Looking in classpath from sun.misc.Launcher$AppClassLoader#73d16e93 for /com/sun/jna/win32-x86-64/jnidispatch.dll
Found library resource at jar:file:/D:/WorkSpace/sample/lib/jna-4.1.0.jar!/com/sun/jna/win32-x86-64/jnidispatch.dll
Looking for library 'libtesseract304'
Adding paths from jna.library.path: /lib/win32-x86-64;C:\Users\PANDORA\AppData\Local\Temp\tess4j\win32-x86-64
Trying C:\Users\PANDORA\AppData\Local\Temp\tess4j\win32-x86-64\libtesseract304.dll
Adding system paths: []
Trying C:\Users\PANDORA\AppData\Local\Temp\tess4j\win32-x86-64\libtesseract304.dll
Looking for lib- prefix
Trying liblibtesseract304.dll
Looking in classpath from sun.misc.Launcher$AppClassLoader#73d16e93 for libtesseract304
Found library resource at jar:file:/D:/WorkSpace/sample/lib/tess4j-3.2.1.jar!/win32-x86-64/libtesseract304.dll
java.lang.UnsatisfiedLinkError: Can not find the specified moduleat com.sun.jna.Native.open(Native Method)at com.sun.jna.Native.open(Native.java:1759)at com.sun.jna.NativeLibrary.loadLibrary(NativeLibrary.java:260)at com.sun.jna.NativeLibrary.getInstance(NativeLibrary.java:398)at com.sun.jna.Native.register(Native.java:1396)at com.sun.jna.Native.register(Native.java:1156

use msdeploy to install a zip-package on a remote computer from commandline

I have a .zip package that I want to install on a development server from my development machine. So I use the msdeploy to do this automatic for me.
msdeploy.exe -verb:sync -source:package=Debug_Services_14.02.20.1413.zip -dest:auto,computername=DEVELOPMENTSERVER,username=ADMIN_USER,password=ADMIN_PWD
But it fails saying that the ERROR_SITE_DOESNT_EXIST.
Info: Adding sitemanifest (sitemanifest).
Info: Adding createApp (MY_SERVICE).
Info: Adding contentPath (MY_SERVICE).
Error Code: ERROR_SITE_DOES_NOT_EXIST
More Information: Site MY_SERVICE does not exist. Learn more at: http
://go.microsoft.com/fwlink/?LinkId=221672#ERROR_SITE_DOES_NOT_EXIST.
Error count: 1.
But I am trying to install it for the first time! What have I missed?
For example. msdeploy api c#.
Execute MSDeploy from C# program code like an API
public static void AppSynchronization(DeploymentBaseOptions depBaseOptions, string appPath)
{
var deploymentObjectSyncApp = DeploymentManager.CreateObject(
DeploymentWellKnownProvider.Package,
appPath, new DeploymentBaseOptions());
deploymentObjectSyncApp.SyncTo(DeploymentWellKnownProvider.Auto, string.Empty,
depBaseOptions, new DeploymentSyncOptions());
}
where
var deployBaseOptions = new DeploymentBaseOptions
{
ComputerName = #"https://WIN-CCDDFDFDFD:8172/msdeploy.axd",
UserName = #"WIN-CCDDFDFDFD\Al",
Password = "1212121",
AuthenticationType = "Basic"
};
appPath = "C:\mySite.zip";

Resources