C# console log

System.Diagnostics.Debug.WriteLine(“MESSAGE”);

Posted by / May 28, 2014 / Posted in C#

C# get app settings

ConfigurationManager.AppSettings[someKey] -> return someValue

<configuration>

  <appSettings>
    <add key=”someKey” value=”someValue”/>
  </appSettings>

</configuration>

Posted by / May 28, 2014 / Posted in C#

C# get url parameter

HttpContext.Current.Request.UrlReferrer.Query.Substring(1);

ex)
http://000.000.000.000:0000/index.html?A=B&C=D
-> A=B&C=D

Excel get column index from column name

colIndexToAlpha(27) -> return AA

alphaToColIndex(AA) -> return 27

// get cellReference’s column from column index
public string colIndexToAlpha(int colIndex) {
  List<string> colNameList = new List<string>();
  int num1 = Convert.ToInt32(colIndex) – 1;
  int num2 = 0;
  int num3 = 0;
  while (true) {
    num2 = num1 / 26;
    num3 = num1 – 26 * num2;
    colNameList.Insert(0, numToAlpha(num3));
    if (num2 < 26) {
      colNameList.Insert(0, numToAlpha(num2 – 1));
      break;
    }
    num1 = num2;
  }
  return String.Join(“”, colNameList.ToArray());
}

private String numToAlpha(int number) {
  if (number < 0) {
    return “”;
  }
  Char c = (Char)(65 + number);
  return c.ToString();
}

// get column index from cellReference’s column
public int alphaToColIndex(string name) {
  int index = 0;
  for (int i = 0; i < name.Length; i++) {
    index *= 26;
    index += alphaToNum(name[i]);
  }
  return index;
}

private int alphaToNum(char alpha) {
  return alpha – 64;// A -> 1 Z -> 26
}

private int alphaToNum(string alpha) {
  if (alpha.Length != 1) {
    return 0;
  }
  return alpha[0] – 64; // A -> 1 Z -> 26
}

Excel get row index and column name from cell referance

private Regex regexRow = new Regex(@”d+”);

public int getRowIndex(string cellReferance) {
  return int.Parse(regexRow.Match(cellName).Value);
}

private Regex regexCol = new Regex(“[A-Za-z]+”);

private string getColumnName(string cellReferance) {
  return regexCol.Match(cellName).Value;
}

C# -> JS all files and directories

get all files and directories in C# and set all files and directories in JS

C#
GetFilesAndDirectories(@”C:”, true);

private List<object> GetFilesAndDirectories(string path, bool isRoot = false) {
  List<object> fileList = new List<object>();
  Dictionary<string, object> fileItem = new Dictionary<string, object>();
  if (isRoot) {
    fileItem = new Dictionary<string, object>();
    fileItem[“type”] = “dir”;
    fileItem[“value”] = path.Substring(path.LastIndexOf(Path.DirectorySeparatorChar) + 1);
    fileItem[“path”] = path;
    fileItem[“fileList”] = GetFilesAndDirectories(path);
    fileList.Add(fileItem);
  } else {
    foreach (string dir in Directory.GetDirectories(path)) {
      fileItem = new Dictionary<string, object>();
      fileItem[“type”] = “dir”;
      fileItem[“value”] = dir.Substring(dir.LastIndexOf(Path.DirectorySeparatorChar) + 1);
      fileItem[“path”] = dir;
      fileItem[“fileList”] = GetFilesAndDirectories(dir);
      fileList.Add(fileItem);
    }
    foreach (string file in Directory.GetFiles(path)) {
      fileItem = new Dictionary<string, object>();
      fileItem[“type”] = “file”;
      fileItem[“value”] = Path.GetFileName(file);
      fileItem[“path”] = file;
      fileItem[“exe”] = Path.GetExtension(file);
      fileList.Add(fileItem);
    }
  }
  return fileList;
}

JS
getFileInfo (c#returnedFileList, true);

var getFileInfo = function (fileList, isRoot) {
  var rtnHtml = “”;
  if ($.isArray(fileList)) {
    rtnHtml += isRoot || false ? ‘<ul>’ : ‘<ul style=”display:none;”>’;
    for (var i = 0, iLen = fileList.length; i < iLen; i++) {
      var fileItem = fileList[i];
      if (fileItem[“type”] === “dir”) {
        rtnHtml += ‘<li><div class=”directory”>’ + fileItem[“value”] + ‘</div>’;
        if (fileItem[“fileList”].length !== 0) {
          rtnHtml += getFileInfo(fileItem[“fileList”]);
        }
      } else if (fileItem[“type”] === “file”) {
        rtnHtml += ‘<li><div class=”file”>’ + fileItem[“value”] + ‘</div></li>’;
      }
    }
    rtnHtml += ‘</ul>’;
  }
  return rtnHtml;
};

C# File download 日本語ファイル名 in IE

 string filename = “test.xls”;
 string filePath = “c:\text.xls”;

 FileStream sourceFile = new FileStream(filePath, FileMode.Open);
 float FileSize;
 FileSize = sourceFile.Length;
 byte[] getContent = new byte[(int)FileSize];
 sourceFile.Read(getContent, 0, (int)sourceFile.Length);
 sourceFile.Close();

 HttpContext.Current.Response.Buffer = true;
 HttpContext.Current.Response.Clear();
 HttpContext.Current.Response.ClearHeaders();
 HttpContext.Current.Response.ClearContent();
// 日本語ファイル名対応(japanese filename support)
 HttpContext.Current.Response.HeaderEncoding = System.Text.Encoding.GetEncoding(“shift_jis”);
 HttpContext.Current.Response.AddHeader(“Content-Disposition”, “attachment; filename=” + filename);
 HttpContext.Current.Response.ContentType = “application/octet-stream”;
 HttpContext.Current.Response.BinaryWrite(getContent);
 HttpContext.Current.Response.End();

// IEでは.NET利用し、システム(OS)言語によりエンコードを勝手に判断するらしい。

Posted by / November 29, 2013 / Posted in C#