![](/img/trans.png)
[英]How to create shortcut of my project .exe in the same folder were .exe is located?
[英]How do i include batch files inside of my .exe Instead of needing them in the folder the .exe is located in
所以一直在搜索或 web 但似乎找不到對我有幫助的答案。 我已經找了將近一個星期了。
我在 vs 中創建了一個程序,以及一些批處理文件。 當將批處理文件包含在帶有 .exe 的文件夾中時,它們可以自行運行,並且通過調試/發布運行良好。
我的問題是我希望只能從我的版本中獲得 .exe 文件,它仍然可以工作。
有沒有辦法可以在.exe 中構建這些文件? 我嘗試使用 c# 來編寫我的控制台命令,而不是包含單獨的批處理文件。 但是我對 c# 很陌生,除了我想運行的命令/如果我運行多行時,我什么也得不到。
我寧願只擁有 c# 而不是包含批處理文件,但我似乎無法找到解決方案。
任何幫助,將不勝感激。
這是我目前調用的批處理文件,效果很好。 同樣,如果有一種方法可以在 c# 中編寫而不是調用批處理文件,我會很樂意學習。
Process process = new Process();
ProcessStartInfo psi = new ProcessStartInfo();
psi.CreateNoWindow = false;
psi.Verb = "runas";
psi.FileName = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"/" + "undo.bat";
psi.UseShellExecute = true;
process.StartInfo = psi;
_ = process.Start();
process.WaitForExit();
我很快就會開始 CyberSecurity 並在我的電腦上玩一些安全的東西。 下面是我的批處理文件中用於啟用安全協議的示例代碼。 如果有的話,我將如何在 c# 中寫這個?
echo ...
echo Enabling Windows Firewall
netsh advfirewall set allprofiles state on
echo Enalbing HyperVisor
bcdedit /set hypervisorlaunchtype auto
echo Enabling UAC
%windir%\System32\reg.exe ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 1 /f
echo.
echo.
echo Your Computer will now be restarting for changes to take effect!
timeout 10
shutdown /r /t 001
您可以做的是將批處理文件作為嵌入資源包含在您的項目中。 然后閱讀它們,然后執行它們。
將它們作為嵌入式資源示例...
然后提取...
然后,您可以將文件寫入磁盤並在其上創建進程。 或者有一種方法可以在不將文件寫入磁盤的情況下執行 cmd.exe,但這有點復雜,所以最好的方法是直接寫入磁盤。
我按照上面給出的指南和其他一些指南來讓我的解決方案發揮作用。 嵌入解決方案中的資源,然后我使用以下代碼幾乎創建了能夠編寫它的功能。
private static void Extract(string nameSpace, string outDirectory, string internalFilePath, string resourceName)
{
Assembly assembly = Assembly.GetCallingAssembly();
using (Stream s = assembly.GetManifestResourceStream(nameSpace + "." + (internalFilePath == "" ? "" : internalFilePath + ".") + resourceName))
using (BinaryReader r = new BinaryReader(s))
using (FileStream fs = new FileStream(outDirectory + "//" + resourceName, FileMode.OpenOrCreate))
using (BinaryWriter w = new BinaryWriter(fs))
w.Write(r.ReadBytes((int)s.Length));
}
這是我用來保存,執行然后刪除文件的內容。
Extract("nameSpace", "outDirectory", "internalFilePath", "resourceName");
Process process = new Process();
ProcessStartInfo psi = new ProcessStartInfo();
psi.CreateNoWindow = false;
psi.Verb = "runas";
psi.FileName = @"C:/" + "resourceName";
psi.UseShellExecute = true;
process.StartInfo = psi;
_ = process.Start();
process.WaitForExit();
System.Threading.Thread.Sleep(10);
if ((System.IO.File.Exists(psi.FileName)))
{
System.IO.File.Delete(psi.FileName);
}
請記住我是新手,所以我確信有更好的編寫方法,但這對我有用!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.