c# - How to call shell script in C# vs VB.Net
问题描述
I am new to C#. I used a converter to convert a VB.Net project to C#. I am looking at the errors I am getting in C# now. VB Code:
Dim oShell As Object
Dim oLink As Object
'you don’t need to import anything in the project reference to create the Shell Object
Try
oShell = CreateObject("WScript.Shell")
oLink = oShell.CreateShortcut(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\" & "Cost Allocation Files" & ".lnk")
oLink.TargetPath = "C:\CostAllocationFiles"
oLink.WindowStyle = 1
oLink.Description = "Cost Allocation Files Directory"
oLink.Save()
Catch ex As Exception
MessageBox.Show("Desktop Cost Allocation Files Link CREATION Failed")
End Try
translates to C# code:
object oShell;
object oLink;
try
{
oShell = Interaction.CreateObject("WScript.Shell");
oLink = oShell.CreateShortcut(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\" + "Cost Allocation Files" + ".lnk");
oLink.TargetPath = @"C:\CostAllocationFiles";
oLink.WindowStyle = 1;
oLink.Description = "Cost Allocation Files Directory";
oLink.Save();
}
catch (Exception ex)
{
MessageBox.Show("Desktop Cost Allocation Files Link CREATION Failed");
}
But it produces errors: CS0103 C# The name 'Interaction' does not exist in the current context 'object' does not contain a definition for 'CreateShortcut' and no accessible extension method 'CreateShortcut' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)
So how do you handle something like this (call to shell script, etc...) in C#?
解决方案
推荐阅读
- hive - 在 hive 表中的同一列上进行分区和存储桶
- phpword - 为什么 phpword 忽略 html 列表中的段落样式?
- postgresql - PostgreSQL 在执行“更新”时获取的确切类型的锁是什么?
- extjs - ExtJS 6--将文本字段控件添加到动态网格列标题中
- math - 在画布游戏中使用负角
- php - 在 PHP 中的 query() 中插入 PROCEDURE
- sql - 在 SQL 中有条件地求和
- python - 将布尔值 [True, False, False...] 的 SEQUENCE 更改为位串序列
- php - WooCommerce 主题声明不起作用
- c++ - 使用“new”初始化 unique_ptr 时
自定义删除器是否负责释放该内存?