在这篇文章中,我们将带领您了解c#–在卸载Winform应用程序时完全删除所有文件夹和文件?的全貌,包括卸载窗体form1的语句是的相关情况。同时,我们还将为您介绍有关android–如何在卸载应用程
在这篇文章中,我们将带领您了解c# – 在卸载Winform应用程序时完全删除所有文件夹和文件?的全貌,包括卸载窗体form1的语句是的相关情况。同时,我们还将为您介绍有关android – 如何在卸载应用程序时删除数据?、C# winform 使用 combobox 遍历文件夹内所有文件、c# – 关闭winform应用程序时将对象设置为null通常是个好主意吗?、c# – 关闭WinForm应用程序时的一次性对象生命周期的知识,以帮助您更好地理解这个主题。
本文目录一览:- c# – 在卸载Winform应用程序时完全删除所有文件夹和文件?(卸载窗体form1的语句是)
- android – 如何在卸载应用程序时删除数据?
- C# winform 使用 combobox 遍历文件夹内所有文件
- c# – 关闭winform应用程序时将对象设置为null通常是个好主意吗?
- c# – 关闭WinForm应用程序时的一次性对象生命周期
c# – 在卸载Winform应用程序时完全删除所有文件夹和文件?(卸载窗体form1的语句是)
是否存在我缺少的MSI设置属性或是否需要确定应用程序的根路径并删除重写的卸载方法上的所有内容?
解决方法
这是设计 – 如果用户错误地卸载了您的应用并清除了所有的财务数据或其他什么,您能想象出这种责任吗?或者,如果他们错误地将其他内容保存到您应用的数据文件夹中,并且您的卸载程序将其删除了?
android – 如何在卸载应用程序时删除数据?
我正在开发一个Android应用程序,它在SD卡中创建一个文件夹并存储一些图像.我想在卸载应用程序时删除该文件夹.请指导我.
解决方法:
//为此你需要运行broadcasrReciver并在androidmanifest.xml文件中包含接收器
<receiver android:name="com.android.mobileasap.PackageChangeReceiver">
<intent-filter>
<action android:name="android.intent.action.PACKAGE_REMOVED" />
<data android:scheme="package" />
</intent-filter>
//添加权限
<uses-permission android:name="android.permission.broADCAST_PACKAGE_REMOVED" />
//在那个PackageChangeReceiver中只删除我在下面的代码中删除doc文件的文件
public class PackageChangeReceiver extends broadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
//this.context=context;
Uri data = intent.getData();
Log.d("hi", "Action: " + intent.getAction());
Log.d("hi", "The DATA: " + data);
String action=intent.getAction();
if(Intent.ACTION_PACKAGE_REMOVED.equalsIgnoreCase(action))
{
String PATH = Environment.getExternalStorageDirectory() + "/mycontent_download/";
File file = new File(PATH);
if (file.exists())
{
String listofFiles [] = file.list();
if (listofFiles!=null)
{
if (listofFiles.length>0)
{
int size = listofFiles.length;
for (int i=0; i<size; i++)
{
if (listofFiles[i].substring(listofFiles[i].length()-4, listofFiles[i].length()).equalsIgnoreCase(".doc"))
{
File f1 = new File(PATH+listofFiles[i]);
f1.delete();
}
}
}
}
}
C# winform 使用 combobox 遍历文件夹内所有文件
参考:https://www.cnblogs.com/hxh88/p/5814291.html
相关函数解析:
1. 指定目录包含的文件和子目录
DirectoryInfo.GetFiles ():获取目录中(不包含子目录)的文件,返回类型为 FileInfo [],支持通配符查找;
DirectoryInfo.GetDirectories ():获取目录(不包含子目录)的子目录,返回类型为 DirectoryInfo [],支持通配符查找;
DirectoryInfo. GetFileSystemInfos ():获取指定目录下(不包含子目录)的文件和子目录,返回类型为 FileSystemInfo [],支持通配符查找;
2. 如何获取指定文件的基本信息;
FileInfo.Exists:获取指定文件是否存在;
FileInfo.Name,FileInfo.Extensioin:获取文件的名称和扩展名;
FileInfo.FullName:获取文件的全限定名称(完整路径);
FileInfo.Directory:获取文件所在目录,返回类型为 DirectoryInfo;
FileInfo.DirectoryName:获取文件所在目录的路径(完整路径);
FileInfo.Length:获取文件的大小(字节数);
FileInfo.IsReadOnly:获取文件是否只读;
FileInfo.Attributes:获取或设置指定文件的属性,返回类型为 FileAttributes 枚举,可以是多个值的组合
FileInfo.CreationTime、FileInfo.LastAccessTime、FileInfo.LastWriteTime:分别用于获取文件的创建时间、访问时间、修改时间;
相关代码:
public static void bindAllFiles(string path, ComboBox comboxlist)
{
List<String> list = new List<string>();
//遍历文件夹
DirectoryInfo theFolder = new DirectoryInfo(path);
FileInfo[] thefileInfo = theFolder.GetFiles("*.*", SearchOption.TopDirectoryOnly);
foreach (FileInfo NextFile in thefileInfo) //遍历文件
list.Add(NextFile.Name);
//遍历子文件夹
DirectoryInfo[] dirInfo = theFolder.GetDirectories();
foreach (DirectoryInfo NextFolder in dirInfo)
{
//list.Add(NextFolder.ToString());
FileInfo[] fileInfo = NextFolder.GetFiles("*.*", SearchOption.AllDirectories);
foreach (FileInfo NextFile in fileInfo) //遍历文件
list.Add(NextFolder.Name+"\\"+NextFile.Name);
}
comboxlist.DataSource = list;//绑定
comboxlist.SelectedIndex = -1;
}
c# – 关闭winform应用程序时将对象设置为null通常是个好主意吗?
我的项目中的一个类叫做Job,它存储String和Lists类型等.是否有必要这样做:
if (Job != null) { Job = null; }
或者这仅对非托管资源是必要的,例如具有dispose方法的文件句柄.我绝对应该调用它,然后设置为null.
任何澄清都会很棒.谢谢.
解决方法
即使您的进程不打算终止,通常也不应将变量设置为null.几乎在所有情况下,垃圾收集器都会做你想要的.如果你知道变量本身仍然是“实时”,你只需要将一个变量设置为null,但是你不希望它当前引用的对象被该变量保持活着状态.这非常罕见.
c# – 关闭WinForm应用程序时的一次性对象生命周期
using (conn = OpenConnection()) { using (tran = conn.BeginTransaction()) { // Do stuff } }
现在,一些用户是住院病人,他们只是在应用程序在后台线程中有活动事务时关闭应用程序.我们的数据库管理员发现这些事务在数据库上仍然活动了几分钟甚至更长时间,这是一个问题,因为打开事务会锁定数据.
那么,当用户决定关闭应用程序时,这些线程中的线程和一次性对象会发生什么?线程是使用Task.StartNew()方法创建的.
解决方法
Foreground and Background Threads:
a background thread does not keep the managed execution environment running. Once all foreground threads have been stopped in a managed process (where the .exe file is a managed assembly),the system stops all background threads and shuts down.
这也适用于线程池中的线程,这些线程在启动新任务时默认使用,而不指定其他选项:
Task Schedulers
The default task scheduler is based on the .NET Framework 4 ThreadPool
Foreground and Background Threads:
Threads that belong to the managed thread pool (that is,threads whose IsThreadPoolThread property is true) are background threads.
关于申请终止的事实
在那之后,我还没有找到任何进一步的信息,在线程终止后发生了什么.我所做的,是简单的测试:
static void Main(string[] args) { Task.Factory.StartNew(Method); Task.Delay(1000); } static void Method() { try { while (true) { } } finally { File.WriteallText(@"C:\polygon\Test.txt","test"); } }
答案是,最终块没有被执行.
解决方案
ThreadAbortException – 不在这里
像ThreadAbortException这样的.NET中有一些功能称为线程终止的答案,但是:
When the common language runtime (CLR) stops background threads after all foreground threads in a managed executable have ended,it does not use Thread.Abort.
Process.Exited事件 – 不在这里
如果您运行其他进程,可以使用以下代码:
var myProcess = new Process(); myProcess.EnableRaisingEvents = true; myProcess.Exited += new EventHandler(CleanUp);
但是,对于当前流程,这也不起作用.
AppDomain.CurrentDomain.ProcessExit – 有效
但是,Avneesh建议的解决方案适用于当前流程:
static void Main(string[] args) { AppDomain.CurrentDomain.ProcessExit += CleanUp; Task.Factory.StartNew(Method); Task.Delay(1000); } static void Method() { try { while (true) { } } finally { CleanUp(null,EventArgs.Empty); } } static void CleanUp(object sender,EventArgs args) { File.WriteallText(@"C:\polygon\Test.txt","test"); }
流程终止
有关rosources的问题的答案可以在Terminating a Process文章中找到:
- Any remaining threads in the process are marked for termination.
- Any resources allocated by the process are freed.
- All kernel objects are closed.
- The process code is removed from memory.
- The process exit code is set.
- The process object is signaled.
以及如何终止进程:
- Any thread of the process calls the ExitProcess function. Note that some implementation of the C run-time library (CRT) call ExitProcess if the primary thread of the process returns.
- The last thread of the process terminates.
- Any thread calls the TerminateProcess function with a handle to the process.
- For console processes,the default console control handler calls ExitProcess when the console receives a CTRL+C or CTRL+BREAK signal.
- The user shuts down the system or logs off.
数据库连接
连接是一种资源,因此如果处理终止,则释放其所有资源.我检查了它在实践中的样子.
C#代码:
sqlConnection connection = new sqlConnection("Data Source=.;Initial Catalog=[database_name];Integrated Security=True"); connection.open(); Debugger.Launch(); connection.Close(); connection.dispose();
会话查找的T-SQL查询:
SELECT COUNT(*) FROM sys.dm_exec_sessions WHERE nt_user_name = '[user_name]' AND program_name = '.Net sqlClient Data Provider'
我注意到,对于每个新连接,都创建了新会话.最有趣的是,在明确关闭或处理连接后,该会话未被释放.但是 – 目前我们感兴趣的领域 – 会话在申请流程终止时被释放.
关于c# – 在卸载Winform应用程序时完全删除所有文件夹和文件?和卸载窗体form1的语句是的问题就给大家分享到这里,感谢你花时间阅读本站内容,更多关于android – 如何在卸载应用程序时删除数据?、C# winform 使用 combobox 遍历文件夹内所有文件、c# – 关闭winform应用程序时将对象设置为null通常是个好主意吗?、c# – 关闭WinForm应用程序时的一次性对象生命周期等相关知识的信息别忘了在本站进行查找喔。
本文标签: