我编写了一个应用程序,它启动一个 3rd 方程序,该程序从我的程序上的电视频道下载媒体流。我使用的程序是 rtmpdump.exe。它们作为独立进程运行,如下所示:
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = path + rtmpdump;
startInfo.Arguments = rtmpdump_argument;
startInfo.WorkingDirectory = path;
startInfo.WindowStyle = ProcessWindowStyle.Minimized;
Process p = Process.Start(startInfo);
Run Code Online (Sandbox Code Playgroud)
但是我想命名这些进程,所以如果我的程序崩溃或必须重新启动,那么我可以检查当前进程的名称,看看是否有一个名为“BBC World - News at 7”的进程。类似的东西来识别哪些已经启动并且当前正在运行。是否可以?我找不到设置友好名称的方法。
我正在尝试使用Word文档中的一些嵌入式对象。较早的海报告诉我,这不是直截了当的。这是链接答案的摘录:
“正如我之前提到的,利用嵌入式对象的编程模型执行保存是一种捷径。还有一种更复杂的解决方案可以与任何嵌入式对象一起使用。为了首先将对象嵌入,必须支持COM IPersist接口之一(即IPersistStorage,IPersistStreamInit,IPersistFile等),因此,始终可以通过在OLEFormat.Object上调用Marshal.QueryInterface(以确定适当的持久性接口)来提取嵌入式对象,并进行相应的转换并然后调用适当的方法。根据所使用的持久性接口,您可能需要调用一些其他方法,以在文件顶部显示适当的存储空间。此外,根据嵌入对象的类型,您可能仍需要先激活对象,然后才能成功为持久性接口使用QueryInterface。”
因此,我有兴趣公开对象正在实现哪个接口。我能找到的最接近的是这里。到目前为止,下面的代码非常感谢Marshal.QueryInterface的任何帮助。
// Opening the word document
object missing = Type.Missing;
this.document = wordApp.Documents.Open(
ref fn, ref confirmConversions, ref readOnly, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing, ref missing);
foreach (Microsoft.Office.Interop.Word.InlineShape inlineShape in this.document.InlineShapes)
{
if (inlineShape.OLEFormat.ProgID != null)
{
switch (inlineShape.OLEFormat.ProgID)
{
// This is a pdf file
case "AcroExch.Document.7":
//Marshal.QueryInterface(IntPtr pUnk, ref Guid iid, out IntPtr ppv); …Run Code Online (Sandbox Code Playgroud) 我正在尝试执行此功能:
public static int QueryInterface(
IntPtr pUnk,
ref Guid iid,
out IntPtr ppv
)
Run Code Online (Sandbox Code Playgroud)
在哪里
pUnk
Type: System.IntPtr
The interface to be queried.
Run Code Online (Sandbox Code Playgroud)
基本上,Marshal.QueryInterface 从 COM 对象请求一个指向指定接口的指针。我想查询许多接口(全部来自 IPersist),那么我如何获取指向这些接口的引用指针呢?
注意:IPersistStorage 就是其中之一。
编辑(这有效):
// Use the CLSID to instantiate the COM object using interop.
Type type = Type.GetTypeFromCLSID(myGuid);
Object comObj = Activator.CreateInstance(type);
// Return a pointer to the objects IUnknown interface.
IntPtr pIUnk = Marshal.GetIUnknownForObject(comObj);
IntPtr pInterface;
Int32 result = Marshal.QueryInterface(pIUnk, ref myGuid, out pInterface);
Run Code Online (Sandbox Code Playgroud) 我最近试图更改我的Windows服务的显示名称,但显然我搞砸了.旧版本无法完全卸载,我无法安装新版本的Windows服务.这是运行installutil/i myService时产生的错误:
An exception occurred during the uninstallation of the System.Diagnostics.EventLogInstaller installer.
System.InvalidOperationException: The event log source '7 Ac Service' cannot be deleted, because it's equal to the log name.
An exception occurred while uninstalling. This exception will be ignored and the uninstall will continue. However, the application might not be fully uninstalled after the uninstall is complete.
Run Code Online (Sandbox Code Playgroud)
因此事件日志源等于日志名称.这是什么意思,我该如何解决?
获取正在使用的Excel电子表格范围的标准方法是UsedRange方法.我可以像这样复制所有使用过的单元格:
xlWorkSheet.UsedRange.Copy(misValue);
Run Code Online (Sandbox Code Playgroud)
不幸的是,这不是一个好方法,因为如果用户在一个中写入内容然后再次删除它,则"激活"单元格.它可能会产生无意识的后果,标记出数千个空行和列(并在我的情况下打印出来).
所以我已将此方法转换为C#以获得更准确的范围.在调试时,它给出FirstRow = 1,FirstColumn = 1,LastRow = 600,LastColumn = 2,这是我想要使用我的测试工作表作为输入.
但是我仍然需要设置实际使用的范围并复制它.在VB中,它是这样完成的:
Set RealUsedRange = Range(Cells(FirstRow, FirstColumn), Cells(LastRow, LastColumn))
Run Code Online (Sandbox Code Playgroud)
如何设置范围并将其复制到C#中?我想替换这一行:
xlWorkSheet.UsedRange.Copy(misValue);
Run Code Online (Sandbox Code Playgroud)
具有(1,1)到(600,2)范围.
我试过这个:
return Excel.Range(xlWorkSheet.Cells[FirstRow, FirstColumn], xlWorkSheet.Cells[LastRow, LastColumn]);
Run Code Online (Sandbox Code Playgroud)
但它给Excel.Range是一个'类型',它在给定的上下文中无效.我不知道写它的正确方法.
// this struct is just to make the result more readable
public struct RealRange
{
public int FirstRow;
public int FirstColumn;
public int LastRow;
public int LastColumn;
public RealRange(int fr, int fc, int lr, int lc)
{
FirstRow = fr;
FirstColumn = fc;
LastRow = lr;
LastColumn = …Run Code Online (Sandbox Code Playgroud) 我已经通过新的MSI版本安装了Windows服务.但是服务只是在没有解释的情况下停止之前完成了构造函数.我在融合日志中得到以下内容:
*** Assembly Binder Log Entry (2013-07-17 @ 12:48:29) ***
The operation failed.
Bind result: hr = 0x80070002. The system cannot find the file specified.
Assembly manager loaded from: C:\Windows\Microsoft.NET\Framework64\v2.0.50727\mscorwks.dll
Running under executable C:\Program Files\SoW\Market Communicator\Company.Market.Communicator.exe
--- A detailed error log follows.
=== Pre-bind state information ===
LOG: User = SoW\kasper
LOG: DisplayName = Company.Market.Administration, Version=1.3.0.49, Culture=neutral, PublicKeyToken=null
(Fully-specified)
LOG: Appbase = file:///C:/Program Files/SoW/Market Communicator/
LOG: Initial PrivatePath = NULL
LOG: Dynamic Base = NULL
LOG: Cache Base = NULL …Run Code Online (Sandbox Code Playgroud) 我有一个必须复制到新目录的文件列表.所有信息都在文件名中.每个原始文件名由[安装号] [新文件名]组成.例如:
235623bob.txt
这里的安装号是235623,新的文件名是bob.txt.安装号为1到11位,新文件名永远不会以数字开头.但它可以从任何其他合法字符开始,它可以包含第一个字符后面的数字.例如:
3245_6786bil54.txt
安装号为3245,新文件名为_6786bil54.txt.我尝试过以下操作:
private void BtnGo_Click(object sender, EventArgs e)
{
string inst_no = ""; // installation number
string dest_filename = ""; // destination filename
string dest_directory = "";
string[] source_files = Directory.GetFiles(TxtSource.Text);
// copy them to their new destination
foreach (string file in source_files)
{
// source filename contains the instno and dest_filename.
Match match = Regex.Match(file, @"(\d+)(\w+)");
inst_no = match.Groups[0].Value;
dest_filename = match.Groups[1].Value;
dest_directory = TxtDestination.Text + "\\" + inst_no;
if (!Directory.Exists(dest_directory))
Directory.CreateDirectory(dest_directory);
File.Copy(file, dest_directory + …Run Code Online (Sandbox Code Playgroud) 我有一个不再运行的Web服务的问题.它以前安装了64位版本,但现在安装了32位版本.但是,它似乎正在寻找一个64位版本的ChilkatDotNet2.dll,这很奇怪.MSI包安装的版本是X86.
C:\ Program Files(x86)\ Sipp\Market Server\bin> asminfo.exe ChilkatDotNet2.dll ChilkatDotNet2,Version = 9.3.0.0,Culture = neutral,PublicKeyToken = eb5fc1fc52ef09b d | X86
我启用了FusionLog,它提供了以下输出:
描述:执行当前Web请求期间发生未处理的异常.请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息.
异常详细信息:System.BadImageFormatException:无法加载文件或程序集"ChilkatDotNet2"或其依赖项之一.尝试加载格式不正确的程序.
来源错误:
在执行当前Web请求期间生成了未处理的异常.可以使用下面的异常堆栈跟踪来识别有关异常的起源和位置的信息.
装配负载跟踪:以下信息有助于确定无法装入装配"ChilkatDotNet2"的原因.
===预绑定状态信息===日志:用户= SF\sonsupport日志:DisplayName = ChilkatDotNet2(部分)日志:Appbase =文件:/// C:/ Program Files(x86)/ Sipp/Market Server/LOG :初始PrivatePath = C:\ Program Files(x86)\ Sipp\Market Server\bin调用程序集:(未知).===日志:此绑定在默认加载上下文中启动.日志:使用应用程序配置文件:C:\ Program Files(x86)\ Sipp\Market Server\web.config日志:使用主机配置文件:C:\ Windows\Microsoft.NET\Framework64\v2.0.50727\Aspnet.config LOG :使用C:\ Windows\Microsoft.NET\Framework64\v2.0.50727\config\machine.config中的计算机配置文件.日志:此时策略未应用于引用(私有,自定义,部分或基于位置的程序集绑定).日志:尝试下载新的URL文件:/// C:/Windows/Microsoft.NET/Framework64/v2.0.50727/Temporary ASP.NET Files/server/b8e4736f/d41d574d/ChilkatDotNet2.DLL.日志:尝试下载新的URL文件:/// C:/Windows/Microsoft.NET/Framework64/v2.0.50727/Temporary ASP.NET Files/server/b8e4736f/d41d574d/ChilkatDotNet2/ChilkatDotNet2.DLL.日志:尝试下载新的URL文件:/// C:/ Program Files(x86)/ Sipp/Market Server/bin/ChilkatDotNet2.DLL.错误:无法完成程序集的设置(hr = 0x8007000b).探测终止.
堆栈跟踪:
[BadImageFormatException:无法加载文件或程序集'ChilkatDotNet2'或其依赖项之一.尝试加载格式不正确的程序.]
System.Reflection.Assembly._nLoad(AssemblyName fileName,String codeBase,Evidence assemblySecurity,Assembly locationHint,StackCrawlMark&stackMark,Boolean throwOnFileNotFound,Boolean forIntrospection)+0
System.Reflection.Assembly .InternalLoad(AssemblyName assemblyRef,Evidence assemblySecurity,StackCrawlMark&stackMark,Boolean forIntrospection)+416
System.Reflection.Assembly.InternalLoad(String assemblyString,Evidence assemblySecurity,StackCrawlMark&stackMark,Boolean forIntrospection)+166 …
我对下面的测试问题的可能答案有一些疑问:
问题:您编写以下代码段以使用平台调用从Win32应用程序编程接口(API)调用函数.
string personName = "N?el";
string msg = "Welcome" + personName + "to club"!";
bool rc = User32API.MessageBox(0, msg, personName, 0);
Run Code Online (Sandbox Code Playgroud)
您需要定义一个最佳编组字符串数据的方法原型.您应该使用哪个代码段?
// A.
[DllImport("user32", CharSet = CharSet.Ansi)]
public static extern bool MessageBox(int hWnd, string text, string caption, uint type);
}
// B.
[DllImport("user32", EntryPoint = "MessageBoxA", CharSet = CharSet.Ansi)]
public static extern bool MessageBox(int hWnd,
[MarshalAs(UnmanagedType.LPWStr)]string text,
[MarshalAs(UnmanagedType.LPWStr)]string caption, uint type);
}
// C. - Correct answer
[DllImport("user32", CharSet = CharSet.Unicode)]
public static extern bool MessageBox(int hWnd, string …Run Code Online (Sandbox Code Playgroud) 以下行抛出FormatException.
DateTime dateResult;
System.Globalization.CultureInfo provider = System.Globalization.CultureInfo.InvariantCulture;
string dateFormat = "yyyy-MM-dd HH:mm";
string dateToCheck = "2013-20-10 00:00";
dateResult = DateTime.ParseExact(dateToCheck, dateFormat, provider); // fails
Run Code Online (Sandbox Code Playgroud)
它说
日历System.Globalization.GregorianCalendar中不支持字符串表示的DateTime.
我看不出有什么不对.