четверг, 11 января 2018 г.

FTP UpLoad File

https://stackoverflow.com/questions/3576436/upload-a-file-with-encoding-using-ftp-in-c-sharp

private static void up(string sourceFile, string targetFile)
{            
    try
    {
        string ftpServerIP = ConfigurationManager.AppSettings["ftpIP"];
        string ftpUserID = ConfigurationManager.AppSettings["ftpUser"];
        string ftpPassword = ConfigurationManager.AppSettings["ftpPass"];
        ////string ftpURI = "";
        string filename = "ftp://" + ftpServerIP + "//" + targetFile; 
        FtpWebRequest ftpReq = (FtpWebRequest)WebRequest.Create(filename);
        ftpReq.UseBinary = true;
        ftpReq.Method = WebRequestMethods.Ftp.UploadFile;
        ftpReq.Credentials = new NetworkCredential(ftpUserID, ftpPassword);

        byte[] b = File.ReadAllBytes(sourceFile);

        ftpReq.ContentLength = b.Length;
        using (Stream s = ftpReq.GetRequestStream())
        {
            s.Write(b, 0, b.Length);
        }

        FtpWebResponse ftpResp = (FtpWebResponse)ftpReq.GetResponse();

        if (ftpResp != null)
        {
            MessageBox.Show(ftpResp.StatusDescription);
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.ToString());
    }
}

Character Encoding during FTP transfer

http://support.2brightsparks.com/knowledgebase/articles/726108-character-encoding-during-ftp-transfer

While using FTP transfers, SyncBack users sometimes encounter problem with misrepresentation of non-English characters in file names such as umlauts (a mark used over a vowel), accented letters, Chinese or Arabic characters.  
FTP is an old protocol (first designed in 1971 with the current protocols from 1985) and the support for non-English characters in file names were not considered when the protocol was initially designed. Therefore, it was designed to be used with English characters only (7-bit ASCII). Later the protocol was extended to use UTF-8 as the character set to support non-English characters in file names.   
SyncBack is fully compliant with the extended specifications and uses UTF-8 (Unicode) if the FTP server supports it. However, if you experience problems with FTP transfer using SyncBack, such as copying file names containing non-English characters to your FTP server or file names are shown with wrong characters after they are transferred to your FTP server, then it could be due to one or more of the following reasons:
  1. Your FTP server follows the original specifications and supports English characters only and rejects those file names with foreign characters. 
  2. Your FTP server violates the specifications and uses custom encoding that SyncBack does not understand.
  3. If your FTP server states that it is Unicode compatible, then SyncBack will use UTF-8 to transfer files. But if you notice that the file names are shown with wrong characters on your FTP server, then it is possible that the FTP client you are using to view the file names is not Unicode compatible or does not understand Unicode, and hence it displays the file names with wrong characters. Alternatively, you could use an FTP client that is Unicode compatible or download that file from your FTP server using SyncBack and verify the file names on your local drive.
Note that if your FTP Server or client (used to view the transferred files) are not Unicode compatible then please upgrade to FTP software that uses UTF-8 (for example FileZilla - https://filezilla-project.org/) or refrain from using foreign characters.

https://winscp.net/eng/docs/ui_login_environment#utf

четверг, 4 января 2018 г.

With Clause with regex

http://www.sql.ru/forum/921216/regexp-udalenie-podstroki-iz-stroki-mezhdu-dvumya-shablonami

https://dev.mysql.com/doc/refman/8.0/en/with.html

WITH cte1 AS (SELECT a, b FROM table1), cte2 AS (SELECT c, d FROM table2) SELECT b, d FROM cte1 JOIN cte2 WHERE cte1.a = cte2.c;


with s as(select '"select j.FIO \"Фамилия, Имя Отчество\", PERSON_ID \"ID человека\""' str from dual)
select str,
regexp_replace(str, '\\"[^\"]*\\"', '')
from s;
StellaMaris

SELECT a.title, a.fulltext FROM maris01.xx2sk_content a WHERE a.fulltext LIKE '%<!--58dba6d87ab6e1490790104-->%' OR a.fulltext LIKE '%<!--/58dba6d87ab6e1490790104-->%'
;

воскресенье, 31 декабря 2017 г.

Assembly Loading

https://stackoverflow.com/questions/658498/how-to-load-an-assembly-to-appdomain-with-all-references-recursively

Once you pass the assembly instance back to the caller domain, the caller domain will try to load it! This is why you get the exception. This happens in your last line of code:

domain.Load(AssemblyName.GetAssemblyName(path));
Thus, whatever you want to do with the assembly, should be done in a proxy class - a class which inherit MarshalByRefObject.
Take in count that the caller domain and the new created domain should both have access to the proxy class assembly. If your issue is not too complicated, consider leaving the ApplicationBase folder unchanged, so it will be same as the caller domain folder (the new domain will only load Assemblies it needs).
public void DoStuffInOtherDomain()
{
    const string assemblyPath = @"[AsmPath]";
    var newDomain = AppDomain.CreateDomain("newDomain");
    var asmLoaderProxy = (ProxyDomain)newDomain.CreateInstanceAndUnwrap(Assembly.GetExecutingAssembly().FullName, typeof(ProxyDomain).FullName);

    asmLoaderProxy.GetAssembly(assemblyPath);
}

class ProxyDomain : MarshalByRefObject
{
    public void GetAssembly(string AssemblyPath)
    {
        try
        {
            Assembly.LoadFrom(AssemblyPath);
            //If you want to do anything further to that assembly, you need to do it here.
        }
        catch (Exception ex)
        {
            throw new InvalidOperationException(ex.Message, ex);
        }
    }
}
If you do need to load the assemblies from a folder which is different than you current app domain folder, create the new app domain with specific dlls search path folder.
For example, the app domain creation line from the above code should be replaced with:
var dllsSearchPath = @"[dlls search path for new app domain]";
AppDomain newDomain = AppDomain.CreateDomain("newDomain", new Evidence(), dllsSearchPath, "", true);
This way, all the dlls will automaically be resolved from dllsSearchPath.
[STAThread]
static void Main(string[] args)
{
    fileDialog.ShowDialog();
    string fileName = fileDialog.FileName;
    if (string.IsNullOrEmpty(fileName) == false)
    {
        AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;
        if (Directory.Exists(@"c:\Provisioning\") == false)
            Directory.CreateDirectory(@"c:\Provisioning\");

        assemblyDirectory = Path.GetDirectoryName(fileName);
        Assembly loadedAssembly = Assembly.LoadFile(fileName);

        List<Type> assemblyTypes = loadedAssembly.GetTypes().ToList<Type>();

        foreach (var type in assemblyTypes)
        {
            if (type.IsInterface == false)
            {
                StreamWriter jsonFile = File.CreateText(string.Format(@"c:\Provisioning\{0}.json", type.Name));
                JavaScriptSerializer serializer = new JavaScriptSerializer();
                jsonFile.WriteLine(serializer.Serialize(Activator.CreateInstance(type)));
                jsonFile.Close();
            }
        }
    }
}

static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
{
    string[] tokens = args.Name.Split(",".ToCharArray());
    System.Diagnostics.Debug.WriteLine("Resolving : " + args.Name);
    return Assembly.LoadFile(Path.Combine(new string[]{assemblyDirectory,tokens[0]+ ".dll"}));
}