gpt4 book ai didi

c# - 在此目录中执行批处理文件

转载 作者:行者123 更新时间:2023-11-30 12:55:29 24 4
gpt4 key购买 nike

我的 C# 窗体项目中使用了这段代码。我也有 exp.bat 文件包含如下 shell 命令。但无论我做什么,它都不会在工作目录中创建 .txt 文件。

@echo off
echo "hello" > test.txt
path = @"‪C:\Users\abc\Desktop\exp.bat";
startingPath = @"C:\Users\abc\Desktop\";
bool success = false;
try
{
System.Diagnostics.ProcessStartInfo ProcStartInfo = new
System.Diagnostics.ProcessStartInfo("cmd");
ProcStartInfo.RedirectStandardOutput = true;
ProcStartInfo.UseShellExecute = false;
ProcStartInfo.CreateNoWindow = false;
ProcStartInfo.RedirectStandardError = true;
System.Diagnostics.Process MyProcess = new System.Diagnostics.Process();
ProcStartInfo.Arguments = "/c start /wait"+path;
ProcStartInfo.WorkingDirectory = startingPath;
MyProcess.StartInfo = ProcStartInfo;
success = MyProcess.Start();
MyProcess.WaitForExit();
}
catch (Exception ex) { string s = ex.StackTrace.ToString();}

最佳答案

最初由评论中的 use Mofi 发布。

发布相同的答案,所以这个问题不计入未回答,问题作者还在评论中确认 Mofi 的答案是正确的并且有帮助。

背景我觉得够了,下面是评论作为答案。

In C# code use the method Environment.GetEnvironmentVariable to get the string value of predefined Windows environment variable USERPROFILE to build the paths for exp.bat and starting directory dynamically already within C# application. Or even better get current user desktop folder directly, see How to get a path to the desktop for the current user in C#?Mofi Feb 22 at 12:25

关于c# - 在此目录中执行批处理文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48880532/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com