यह जांचने के लिए कि कोई फ़ाइल उपयोग में है (खुला) File Stream
का उपयोग करें।
उदाहरण के लिए:
मान लें आप txtAttachPath.Text
में संग्रहीत फ़ाइल पथ है और आप उस फ़ाइल को खोलने के लिए चाहते हैं, तो यह पहले से ही खुला नहीं है, आप अगर फ़ाइल खुला पहले तो इसे खोलने की जाँच करने की जरूरत है, यह है कि कैसे करना है:
जाँच करने के लिए एक विधि बनाने यदि फ़ाइल खुला है:
private bool attachedFileIsOpen(FileInfo file)
{
FileStream fs = null;
try
{
fs = file.Open(FileMode.Open, FileAccess.ReadWrite, FileShare.None);
}
catch (IOException ioe)
{
MessageBox.Show("The attachment file is already open!, can't open more than once!", "AMP Warning", MessageBoxButtons.OK, MessageBoxIcon.Information);
return true;
}
finally
{
if (fs != null) fs.Close();
}
return false;
}
अगर फ़ाइल खुला है विधि की जाँच करें और उपयोगकर्ता के लिए संदेश है कि फाइल aleardy खुला है के साथ सच retrun होगा, अन्यथा यह झूठी वापसी करेगा (फाइल है बेकार)।
अगला विधि किसी भी घटना से चालू होने का उपयोग कर आप चुनते हैं:
FileInfo filePath = new FileInfo(txtAttachPath.Text);
if (!txtAttachPath.Text.Equals("No Attachment")
&& attachedFileIsOpen(filePath) == false
&& processIsRunning("notepad") == false)
{
Process openFilebyExtension = Process.Start(txtAttachPath.Text);
openFilebyExtension.WaitForInputIdle();
NativeWindow.FromHandle(this.Handle);
}
नोट कुछ बिल्ट-इन खिड़कियों कार्यक्रमों नोटपैड की अनुमति देता है की तरह एक ही फाइल के कई उदाहरण खोले जाने के लिए, इस प्रकार का पता नहीं किया जा सकता है File Stream
विधि द्वारा, इसके लिए अन्य समाधान यह पता लगाने के लिए है कि प्रक्रिया चल रही है या नहीं।
आप फ़ाइल प्रकार खोलने का प्रयास कर रहे हैं के साथ जुड़े प्रक्रिया चलाने के लिए जाँच करने के लिए एक और तरीका जोड़ने की आवश्यकता होगी:
private bool processIsRunning(string process)
{
Process[] runningProcesses = Process.GetProcessesByName(process);
bool processIsRunning = false;
if(runningProcesses.Length == 0)
{
processIsRunning = false;
}
else
{
processIsRunning = true;
MessageBox.Show("The attachment file is already open!, can't open more than once!", "AMP Warning",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
return processIsRunning;
}
पूर्ण स्रोत कोड के साथ किसी भी अंतिम समाधान? – Kiquenet