我有一个处理vbscript并生成输出的应用程序.
private static string processVB(string command, string arguments)
{
Process Proc = new Process();
Proc.StartInfo.UseShellExecute = false;
Proc.StartInfo.RedirectStandardOutput = true;
Proc.StartInfo.RedirectStandardError = true;
Proc.StartInfo.RedirectStandardInput = true;
Proc.StartInfo.StandardOutputEncoding = Encoding.UTF8;
Proc.StartInfo.StandardErrorEncoding = Encoding.UTF8;
Proc.StartInfo.FileName = command;
Proc.StartInfo.Arguments = arguments;
Proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; //prevent console window from popping up
Proc.Start();
string output = Proc.StandardOutput.ReadToEnd();
string error = Proc.StandardError.ReadToEnd();
if (String.IsNullOrEmpty(output) && !String.IsNullOrEmpty(error))
{
output = error;
}
//Console.Write(ping_output);
Proc.WaitForExit();
Proc.Close();
return output;
}
Run Code Online (Sandbox Code Playgroud)
我想我已经设置了与编码属性相关的所有内容.processVB方法将获取命令作为VBscript文件及其参数.
C#方法processVB正在处理现在生成输出的VBScript文件,如下所示.
"?"
但我应该得到原文
"äåéö€"
我已正确设置编码.但我无法做到正确.
我究竟做错了什么?
我正在尝试创建一个隐式转换,允许我使用LINQ结果直接返回MyCollection.
public class MyCollection : ICollection<MyType>
{
private List<MyType> _list = new List<MyType>();
public MyCollection(IEnumerable<MyType> collection)
{
_list = new List<MyType>(collection);
}
public static implicit operator MyCollection(IEnumerable<MyType> collection)
{
return new MyCollection(collection);
}
// collection methods excluded for brevity
public MyCollection Filter(string filter)
{
return _list.Where(obj => obj.Filter.Equals(filter)); // cannot implicitly convert
}
}
Run Code Online (Sandbox Code Playgroud)
我之前没有尝试使用隐式用户定义的转换,我做错了什么?
我在我的解析器中抛出自定义异常,但它们被Automapper捕获并包装,所以我们无法在程序的其他地方处理它们.我已经包含了一个简单的问题示例,期望的结果是捕获一个InterfaceError,但它只捕获一个AutoMapperException,并将InterfaceError作为内部异常.
在班上:
public Order MapOrder(InterfaceOrder iOrder)
{
try
{
Order mappedOrder = Mapper.Map<InterfaceOrder, Order>(iOrder);
}
catch (InterfaceException ex)
{
Log("Interface error");
}
catch (Exception ex)
{
Log("error");
}
return mappedOrder;
}
Run Code Online (Sandbox Code Playgroud)
制图:
Mapper.CreateMap<InterfaceOrder, Order>()
.ForMember(c => c.Name, op => op.ResolveUsing(data =>
{
if (Name.Length > 50)
{
throw new InterfaceException("Error!", ex);
}
else
{
return c.Name
}
}));
Run Code Online (Sandbox Code Playgroud) 我可能有点懒,在这里问这个,但我刚开始使用LINQ而且我有一个函数,我确信可以变成两个LINQ查询(或一个嵌套查询)而不是一个LINQ和一对foreach陈述.作为一个例子,任何LINQ大师都会关心为我重构这个吗?
函数本身循环遍历.csproj文件列表,并拉出项目中包含的所有.cs文件的路径:
static IEnumerable<string> FindFiles(IEnumerable<string> projectPaths)
{
string xmlNamespace = "{http://schemas.microsoft.com/developer/msbuild/2003}";
foreach (string projectPath in projectPaths)
{
XDocument projectXml = XDocument.Load(projectPath);
string projectDir = Path.GetDirectoryName(projectPath);
var csharpFiles = from c in projectXml.Descendants(xmlNamespace + "Compile")
where c.Attribute("Include").Value.EndsWith(".cs")
select Path.Combine(projectDir, c.Attribute("Include").Value);
foreach (string s in csharpFiles)
{
yield return s;
}
}
}
Run Code Online (Sandbox Code Playgroud) 我一直在尝试打印到 SATO GL408e 打印机。我正在尝试通过 USB 接口发送数据,但没有示例代码可以执行此操作。我尝试使用此处的示例代码: http: //support.microsoft.com/kb/322091,但它运行时没有错误,也没有打印。有人知道如何将数据发送到 SATO 打印机吗?
谢谢!
(这是我正在使用的代码)
private void PrintLabels_Click(object sender, EventArgs e)
{
string PrintCommand = "";
string EmployeeNo = EmployeeList.SelectedRows[0].Cells[0].Value.ToString();
string YearNumber = EmployeeList.SelectedRows[0].Cells[7].Value.ToString();
string FirstTraceCode = (int.Parse(EmployeeList.SelectedRows[0].Cells[5].Value.ToString()) + 1).ToString();
string SecondTraceCode = (int.Parse(EmployeeList.SelectedRows[0].Cells[5].Value.ToString()) + 2).ToString();
string LabelQuantity = "15";
MessageBox.Show(EmployeeNo.Substring(0, 2) + "-" + EmployeeNo.Substring(2, 4) + "-" + YearNumber + "-" + FirstTraceCode);
PrintCommand = "\x02\x1bA\x1bIG1\x1bZ\x03\x02\x1bA\x1b#E3\x1bZ\x03\x02\x1bA\x1bCS5\x1bZ\x03\x02\x1bA\x1bEX0\x1bZ\x03\x02\x1bA\x1b*&\x1bZ\x03\x02\x1bA\x1bA3H0001V0001\x1bZ\x03\x02\x1bA\x1bV0000\x1bH0259\x1bF001+002\x1bD202099" + EmployeeNo + YearNumber + FirstTraceCode + "\x1bL0203\x1bH0247\x1bV0114\x1bF001+002\x1bU" + EmployeeNo.Substring(0, 2) + "-" + …Run Code Online (Sandbox Code Playgroud) 我正在使用此代码从我的app.config文件中读取连接字符串,但它始终返回空值.我的App.config文件在我的项目下.两种方法都产生空值:
public SqlConnection getConnection()
{
try
{
// connectionString = ConfigurationManager.AppSettings["dbConn"];
connectionString = ConfigurationManager.ConnectionStrings["dbConn"].ConnectionString;
connectionString = System.Configuration.ConfigurationManager.AppSettings["dbConn"];
sqlConnection = new SqlConnection(connectionString);
sqlConnection = new SqlConnection(connectionString);
}
catch (Exception ex)
{
}
return sqlConnection;
}
Run Code Online (Sandbox Code Playgroud)
这是我的app.config文件声明:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<connectionStrings>
<add name="dbConn" providerName="System.Data.SqlClient"
connectionString="Data Source=VANYA\SQLEXPRESS;Initial Catalog=mydatabase;User Id=sa;Password=123" />
</connectionStrings>
</configuration>
Run Code Online (Sandbox Code Playgroud) 使用protobuf-net v2 build 668,我正在尝试序列化/反序列化一个类,该类包含一个定义为接口的成员,同时进行即时转换.通常,代理方法可以正常工作,但由于C#不允许用户定义的接口转换,我无法定义转换.
谢谢,
namespace ProtoBufNetTest
{
using System.Diagnostics;
using System.IO;
using ProtoBuf;
using ProtoBuf.Meta;
class Program
{
static void Main()
{
RuntimeTypeModel.Default.Add(typeof(IDummy), false)
.SetSurrogate(typeof(DummySurrogate));
var container = new Container { Data = new Dummy { Positive = 3 } };
using (var file = File.Create("test.bin"))
{
Serializer.Serialize(file, container);
}
using (var file = File.OpenRead("test.bin"))
{
container = Serializer.Deserialize<Container>(file);
Debug.Assert(container.Data.Positive == 3);
}
}
}
// Outside of the project, cannot be changed
public interface IDummy
{
int Positive …Run Code Online (Sandbox Code Playgroud) 我必须创建一个大的 XLSX 文件。我使用 OpenXmlWriter 尽可能快地完成任务。该文件已正确创建,但我无法更改该字段的数据类型。它始终是标准类型,我想对其中一些使用数字格式。
我尝试了很多方法,但没有一个成功(Cell DataType,带有属性,...)。
这是一个例子:
SpreadsheetDocument fichier_excel = SpreadsheetDocument.Create(chemin + NomFichier, SpreadsheetDocumentType.Workbook);
fichier_excel.AddWorkbookPart();
WorksheetPart wsp = fichier_excel.WorkbookPart.AddNewPart<WorksheetPart>();
OpenXmlWriter writer = OpenXmlWriter.Create(wsp);
writer.WriteStartElement(new Worksheet());
writer.WriteStartElement(new SheetData());
oxa = new List<OpenXmlAttribute>();
oxa.Add(new OpenXmlAttribute("r", null, "1"));
writer.WriteStartElement(new Row(), oxa);
oxa = new List<OpenXmlAttribute>();
oxa.Add(new OpenXmlAttribute("t", null, "str"));
writer.WriteStartElement(new Cell(), oxa);
writer.WriteElement(new CellValue("10001"));
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();
writer.Close();
writer = OpenXmlWriter.Create(fichier_excel.WorkbookPart);
writer.WriteStartElement(new Workbook());
writer.WriteStartElement(new Sheets());
writer.WriteElement(new Sheet()
{
Name = "Inventaire",
SheetId = 1,
Id = fichier_excel.WorkbookPart.GetIdOfPart(wsp)
});
writer.WriteEndElement();
writer.WriteEndElement();
writer.Close();
writer.Dispose(); …Run Code Online (Sandbox Code Playgroud) 作为asp.net中的新功能.在我的asp.net应用程序中log off on click event使用函数的成员身份ClearSession(),但是如果我在浏览器上单击后退按钮,它会在转发到缓存页面后出现问题.如何在浏览器中清除缓存,以便用户在未登录时无法查看其配置文件
protected void ClearSession()
{
FormsAuthentication.SignOut();
Session.Clear();
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ExpiresAbsolute = DateTime.UtcNow.AddDays(-1d);
Response.Expires = -1500;
Response.CacheControl = "no-Cache";
}
Run Code Online (Sandbox Code Playgroud) 我正在写一份在线评估表.在此表格上,用户必须选择至少3人,最多7人将对他们进行评估.我有一个用户添加评估者的表单,我在此表单下方显示列表.一旦用户完成添加评估员,然后单击自我评估按钮以填写他/她自己的自我评估.
我想要做的是验证在用户离开页面之前评估者的数量确实在正确的范围内.
模型是这样的
public class AssessorsViewModel
{
List<Assessor> Assessors { get; set; }
}
public class Assessor
{
string Email { get; set; }
string Name { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我有Assessor类的验证属性,因此每次用户添加评估器时我都可以对此进行验证,但我无法弄清楚如何验证评估者列表上的Count.
我正在使用ASP.net MVC.
提前致谢
c# ×10
asp.net ×3
.net ×2
asp.net-mvc ×1
automapper ×1
c#-4.0 ×1
casting ×1
encoding ×1
linq ×1
linq-to-xml ×1
openxml ×1
openxml-sdk ×1
process ×1
protobuf-net ×1
usb ×1
utf-8 ×1
validation ×1